G
G
Grishan Trudovik2017-03-07 19:12:56
JavaScript
Grishan Trudovik, 2017-03-07 19:12:56

Trying to find the sum of the array elements (with the value entered from the screen). Why am I getting NaN?

var 	mas=[];
    sum=0;
  for (var i =1; i < 4; i++) {
    mas.push(prompt("element №" + i));
    sum = sum + parseInt(mas[i]);
    alert(mas);
    alert(sum);
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nicholas, 2017-03-07
@GoldenLights

Arrays have an index starting at 0.
For debugging, use console.log instead of alert. It is more comfortable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question