How to sum numbers saved as array in JavaScript wi

2019-08-04 17:45发布

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);

6条回答
Juvenile、少年°
2楼-- · 2019-08-04 17:54

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

while(i < numbers.length) 
查看更多
We Are One
3楼-- · 2019-08-04 17:56

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].

查看更多
Viruses.
4楼-- · 2019-08-04 17:59

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.

查看更多
Lonely孤独者°
5楼-- · 2019-08-04 18:01

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);

查看更多
叼着烟拽天下
6楼-- · 2019-08-04 18:02

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! :)

查看更多
Explosion°爆炸
7楼-- · 2019-08-04 18:13

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) {
查看更多
登录 后发表回答