0

i have this code to sum 3 numbers so i+j+k but I get as result Nan

here is the code:

var i = req.param('1', null);
      i = parseInt(i);
      var j = req.param('2', null);
          j = parseInt(j);

      var k = req.param('3', null);
         k = parseInt(k);

      var r = i+j+k;
      res.render('index', {result:r});

this is node based js

4
  • 3
    One or more of the three is not actually a number. Do a console.log on each one before calling parseInt Commented May 25, 2012 at 14:18
  • You should always pass a second argument 10 to parseInt to ensure it parses the number as a decimal value and not e.g. hex (0x prefix) or octal (leading zero) Commented May 25, 2012 at 14:19
  • Maybe you should declare your r variable before i (var r = 0;)and then instead of parsetInt into the same variable everytime, parse it to your r variable doing a sum like r += parseInt(i) and repeat that for j and k.. try that Commented May 25, 2012 at 14:21
  • 1
    @MilkyWayJoe: "...Maybe you should declare your r variable before i..." Won't make any difference, see Poor misunderstood var (Although I always recommend doing all var statements at the top of the execution context, since that's where they happen anyway.) Commented May 25, 2012 at 14:31

4 Answers 4

3

req.param('2', null) returns null if the parameter is not specified.

You then call parseInt(null), which gives NaN

If you add any number with NaN, you get NaN.


Use req.param('2', 0) instead

Sign up to request clarification or add additional context in comments.

Comments

1

You'll see NaN if any of the values in the calculation is NaN. In your example, presumably i, j, and/or k is NaN. You'll need to look to see which it is. Note that parseInt can return NaN (for instance, parseInt(null) is NaN).


Side note: It's almost always best to include the radix when using parseInt, e.g. parseInt(i, 10) rather than just parseInt(i). That way, parseInt won't guess at the radix (number base).

Comments

1

When JavaScript converts a string to an int using parseInt, it reads each character until it sees a non-numeric character. A blank string ('') is non-numeric, so it gets converted to NaN.

Examples:

parseInt('ABC', 10); // NaN
parseInt('123ABC', 10); // 123

So, you need to make sure the input isn't blank; make sure it's actually a number.

P.S. I suggest adding the 2nd parameter to parseInt, it tells it what base to use. If it's excluded, it tries to guess. Sometimes this causes issues:

parseInt('12'); // 12 (base 10)
parseInt('012'); // 10 (the leading '0' makes it base 8 [octal])

parseInt('012', 10); // 12. forced to be base 10

Comments

0

If one of the three variables is NaN, then it's not adding, but concatenating. Do a console.log or alert for each variable to find the individual values to see which one isn't a number.

EDIT...oops, I just saw you are already forcing them to integers. which is good. So the error is actually happening at that step...one (or more) of your values is not a number and therefore can't be forced to an integer.

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.