我正在通过第一章练习。 4个Eloquent JavaScript。 以下是我迄今为止所做的两个功能。函数输出= NAN
//Takes two number parameters and outputs the range into an array.
var rangeArray = [];
function range(start, end) {
for (var i = start; i <= end; ++i)
rangeArray.push(i);
return rangeArray;
}
//Takes the array from above and is supposed to output the sum of the elements of the array.
function sum(range) {
var sumTotal = 0;
for (var index = 0; index <= rangeArray.length; ++index)
sumTotal += rangeArray[index];
return sumTotal;
}
//The above functions are supposed to output the outputs shown below if they work correctly. The first one works, but the second console.log statement gives me an output of NaN.
console.log(range(1, 10));
// → [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
console.log(sum(range(1, 10)));
// → 55
所以,我的第二个功能出了什么问题,但我找不出什么。它是初始参数(范围)吗?或者在某处有语法错误?
下面是我从这些练习中获得这些练习的页面的链接,它们接近页面的底部。 http://eloquentjavascript.net/04_data.html
非常感谢您的帮助!
不要使用<= for循环 - 将其更改为< – jeff