0

I make a function(in javascript) to sum the elements of an array and return the result ... but there is a problem and the output of this function is "NaN" but when i checked the type of variable , that was "number" :| please please help me to solve this ...

function arrSum(a) {
   var val = 0;
   for (var i = 0; i <= a.length; i++) {
      val += a[i];
   }
      return val;
}
var testArr = [1, 2, 3, 4];
var pass = arrSum(testArr);
document.write(pass);

// SHOW "NaN"

function arrSum(a) {
   var val = 0;
   for (var i = 0; i <= a.length; i++) {
      val += a[i];
   }
      return val;
}
var testArr = [1, 2, 3, 4];
var pass = arrSum(testArr);
document.write(typeof pass);

// SHOW "number"

i am sorry about my english. i am not good at it :) and thank you for your helps ...

1
  • Where is result defined? Commented Mar 7, 2018 at 14:31

4 Answers 4

3

Very much simple syntax(for array sum).

var test = [ 1, 2, 3, 4 ];
sum = eval( test.join("+") );  //join will return string which is '1+2+3+4' and eval will evaluate it and give result which is sum of numbers.
console.log(sum);

You are returning result but your sum is in val.

And also for loop condition is incorrect.

The reason you are getting NaN because at last iteration you are doing number+undefined which will result in NaN and type of NaN is number.

Hope it's clear your doubt why you are getting NaN and number.

Below is demo of correct code.

function arrSum(a) {
   var val = 0;
   for (var i = 0; i < a.length; i++) {
      val += a[i];
   }
      return val;
}
var testArr = [1, 2, 3, 4];
var pass = arrSum(testArr);
document.write(pass);

For additional info

Conversion rules

operand + operand = result 
  1. If at least one operand is an object, it is converted to a primitive value (string, number or boolean);
  2. After conversion, if at least one operand is string type, the second operand is converted to string and the concatenation is executed;
  3. In other case both operands are converted to numbers and arithmetic addition is executed.

If both operands are primitive types, then operator checks if at least one is string and executes the concatenation. In other case it just transforms everything to numbers and sum.

Example:-

var result = 12 + undefined; // NaN 

Explanation:

  1. 12 + undefined (Because none of the operands is string, convert the undefined to a number NaN based on rule 3)
  2. 12 + NaN (Numbers addition)
  3. NaN
Sign up to request clarification or add additional context in comments.

2 Comments

It would be helpful to explain them why they're getting NaN and why their typeof prints number
but your explain was very helpful
2

For-loop's ending condition is incorrect, array's index is 0-based

Make it

for (var i = 0; i < a.length; i++) {

Also return val instead of result.

Demo

function arrSum(a) {
   var val = 0;
   for (var i = 0; i < a.length; i++) {
      val += a[i];
   }
      return val; //observe this line
}
var testArr = [1, 2, 3, 4];
var pass = arrSum(testArr);
console.log(pass);

2 Comments

Also, the function returns an undefined variable.
@Amy yeah and that as well. Was just about to submit my edit :)
0

Another way to write arrSum function in one line with no loops using using reduce (ES5):

function arrSum(arr) {
 return arr.reduce((sum, x) => sum + x);
}

var testArr = [1, 2, 3, 4];
var pass = arrSum(testArr);
document.write(pass);

Comments

0

The problems are return result and for (var i = 0; i <= a.length; i++). You must use respectively return val and for (var i = 0; i < a.length; i++).

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.