Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么函数产生错误的解 功能范围(开始、结束){ 变量列表=[] 对于(var count=start;count_Javascript_Arrays_Function - Fatal编程技术网

Javascript 为什么函数产生错误的解 功能范围(开始、结束){ 变量列表=[] 对于(var count=start;count

Javascript 为什么函数产生错误的解 功能范围(开始、结束){ 变量列表=[] 对于(var count=start;count,javascript,arrays,function,Javascript,Arrays,Function,,您的迭代超出了nums数组的边界。 同样,在循环的最后一次迭代中,您有效地执行了total=total+undefined,其结果是NaN。 例如,在JavaScript控制台中,n+undefined会产生NaN,其中n是任意数字 将循环条件更改为count

,您的迭代超出了
nums
数组的边界。 同样,在循环的最后一次迭代中,您有效地执行了
total=total+undefined
,其结果是
NaN
。 例如,在JavaScript控制台中,
n+undefined
会产生
NaN
,其中
n
是任意数字


将循环条件更改为
count
,而不是
,您的迭代超出了
nums
数组的边界。 同样,在循环的最后一次迭代中,您有效地执行了
total=total+undefined
,其结果是
NaN
。 例如,在JavaScript控制台中,
n+undefined
会产生
NaN
,其中
n
是任意数字

将循环条件更改为
count
,而不是
   <script>
    function range (start , end ) {
      var list =[]
      for (var count = start ; count <= end ; count++)
        list.push(count);
      return list
    }

    function sum ( nums ) {
      var total = 0;
      for ( var count = 0 ; count <= nums.length ; count++ )
        total = total + nums[count];
      return total;
    }

    console.log(range(1 , 10))
    console.log(sum(range(1 ,10)))
    </script>
  for ( var count = 0 ; count < nums.length ; count++ )
    total = total + nums[count];