1

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);
3
  • 1
    while(i < numbers.length) 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. 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. Commented Jan 18, 2017 at 20:47

6 Answers 6

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) {
Sign up to request clarification or add additional context in comments.

Comments

3

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.

5 Comments

But they asked what was wrong with their code... not for an alternative. Solve the problem first then offer an alternative.
Thank you @Andrew, I will do that now.
Its a good answer and helps future readers. Please don't drop ES syntax.
@user2330678 Agreed, but solve the problem at hand first. I agree the ES6 syntax is preferred but answer the question first.
I just emphasised it is an alternative, the other answers give a hint of what was wrong in the original solution.
1

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

while(i < numbers.length) 

Comments

1

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

Comments

1

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

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.