Raghav
Raghav

Reputation: 17

Simple add function returns NaN

This is my code:

var i=5;
var j=5;

function sum(n,m){
  var num = n + m ;
  alert(num);

};

sum(i+j);

When I run it, it should return '10', but it returns NaN.

Please help me with what I'm doing wrong.

Upvotes: 0

Views: 50

Answers (2)

Raghav
Raghav

Reputation: 17

I figured what I was doing wrong.

this below code works -

var i=5;
var j=5;

function sum(n,m){
  var num = n + m ;
  alert(num);

};

sum(i, j);

Upvotes: 0

norbitrial
norbitrial

Reputation: 15166

Change it from sum(i + j) to sum(i, j). So technically your code was passing originally like sum(10, undefined) which resolved as NaN.

var i=5;
var j=5;

function sum(n,m){
  var num = n + m ;
  alert(num);

};

sum(i, j);

I hope that helps!

Upvotes: 3

Related Questions