我正在通過第一章練習。 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