最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to sum numbers saved as array in JavaScript with while loop - Stack Overflow

programmeradmin7浏览0评论

Where is the mistake? I want to sum every number in the array. The alert says NaN.

var numbers = [10, 42, 5, 87, 61, 34, 99];
var i = 0; 
var e = 0;
while(i <= numbers.length) {
    e = e + numbers[i]; 
    i++;
}

alert(e);

Where is the mistake? I want to sum every number in the array. The alert says NaN.

var numbers = [10, 42, 5, 87, 61, 34, 99];
var i = 0; 
var e = 0;
while(i <= numbers.length) {
    e = e + numbers[i]; 
    i++;
}

alert(e);
Share Improve this question edited Apr 2, 2017 at 9:23 George Kagan 6,1248 gold badges49 silver badges50 bronze badges asked Jan 18, 2017 at 20:43 user7437796user7437796 3
  • 1 while(i < numbers.length) – user2330678 Commented Jan 18, 2017 at 20:46
  • [42] is an array of length 1. It only has an element at index 0, not at index 1. – Felix Kling Commented Jan 18, 2017 at 20:46
  • 3 "Where is the mistake?" Why do you think there is a mistake? Do you not get what you expect? What do you expect and what do you get? Please read How to Ask for guidance. – Felix Kling Commented Jan 18, 2017 at 20:47
Add a ment  | 

6 Answers 6

Reset to default 4

This line is the reason:

while(i <= numbers.length) {

Arrays are 0 index so you can go from index 0 (inclusive) until numbers.length (exclusive). You are going beyond that limit, causing you to access an element that isn't defined at the given index. You must do this instead:

while(i < numbers.length) {

Alternatively using the ES2015 syntax you can do it like this.

let numbers = [10, 42, 5, 87, 61, 34, 99];
let sum = numbers.reduce((a,b) => a + b);

You can read about Array.prototype.reduce(accumulator, element, callback, startingValue) here.

Your condition is wrong, just use < insteead of <=

while(i < numbers.length) 

There's a few ways you can improve this. The first is that you want to change your condition to i < numbers.length, not i <= numbers.length. As you've written it, the last number will never be counted.

Another way you can improve this is using the += notation -- there's no need to write e = e + numbers[i] when you can write e += numbers[i].

You could íterate from the end of the array with a post increment and a check for truthyness in the while condition.

Inside just add the item.

var numbers = [10, 42, 5, 87, 61, 34, 99],
    i = numbers.length,
    e = 0;

while (i--) {
    e += numbers[i]; 
}

console.log(e);

The problem is in array length You are checking because array index starts with 0. In that way when doing:

i <= numbers.length

You are checking [0: 10, 1: 42, 2: 5, 3: 87, 4: 61, 5: 34, 6: 99, 7:???] because numbers length is 7. To solve it just do:

i < numbers.length 

where You don't check non existing number in array.

Here is how we were thought in school:

//main code//
var numbers = [10, 42, 5, 87, 61, 34, 99];
var result = sumIntArray(numbers);
//end of main code//

function sumIntArray(array){
    var result = 0;
    for (var i = 0; i < array.length; i++) {
        if (array[i].isInteger()) {
            result += array[i];
        } else {
            if (i === 0) {
                alert("1st object in array is not number, will skip...");
            } 
            else if (i === 1) {
                alert("2nd object in array is not number, will skip...");
            }  
            else if (i === 2){
                alert("3rd object in array is not number, will skip...");
            }    
            else {
                alert((i+1).toString() + "th object in array is not number, will skip...");
            }
        } 
    }
    return result;
}

The for loop adds this i++ You make in a while loop and makes code a bit more clear.
No need for declaring i in main code :)

  1. Always make functions to simplify main code.
  2. Make function that will never make mistake even if somebody wants to break The logic.

Although don't make school break Your creativity, Your code is great! :)

发布评论

评论列表(0)

  1. 暂无评论