检查数组是否?

编程入门 行业动态 更新时间:2024-10-25 09:34:11
本文介绍了检查数组是否?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

function sumAll(a) { 'use strict'; var result = 0; for (var i = 0; i < a.length; ++i) { result = result + a[i]; } return result; }

我的任务是修改这段代码,这样当传递的第一个参数不是数组时,函数返回那个论点。我是JS的新手,我不知道如何处理这个问题。 sumAll(18)应该返回18 sumAll('Hello')应该返回'Hello'

My assignment is to modify this code so that when the first argument passed is NOT an array, the function returns that argument. I'm quite new to JS and I'm not on how to approach this. sumAll(18) should return 18 sumAll('Hello') should return 'Hello'

推荐答案

function sumAll(a) { if (Array.isArray(a)) { alert('Yes') } else { alert('No') } }

现在,你有代码确定它是否是一个数组。继续这样做。请记住,作业是为了学习目的。如果我们提供完整的解决方案,您将无法从中学到任何东西。 经常使用Google。您将学习并获得解决方案。 问候..

Now, you have code to determine whether its an array or not. proceed with that. Remember, assignments are meant for learning purpose. If we provide complete solution than you won't learn anything out of it. Use Google often.You will learn as well as get the solution. Regards..

function sumAll(a) { 'use strict'; var result = 0; if(!a){ return a; } if(!a instanceof Array){ return a; } for (var i = 0; i < a.length; ++i) { result = result + a[i]; } return result; }

更多推荐

检查数组是否?

本文发布于:2023-11-22 15:15:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617935.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!