3

I'm creating a little calculator. Part of this will allow you to input upto 4 numbers but you could just have 1,2 or 3 as well. I've got the code working except for when I only enter 1, 2 or 3 numbers I get a NaN result for the numbers I haven't entered.

How do I get rid of the NaN result?

my jQuery for splitting the inputted form data is -

    var numbers = num4.split(" ");
    var firstNumber = parseInt(margins[0], 10),
        secondNumber = parseInt(margins[1], 10),
        thirdNumber = parseInt(margins[2], 10),
        fourthNumber = parseInt(margins[3], 10);
    console.log(firstNumber, secondNumber, thirdNumber, fourthNumber);

    var shorthand1 = parseInt (firstNumber, 10) * 1;
    var shorthand2 = parseInt (secondNumber, 10) * 1;
    var shorthand3 = parseInt (thirdNumber, 10) *1;
    var shorthand4 = parseInt (fourthNumber, 10) *1;

and I just have

shorthand1 + shorthand2 + shorthand3 + shorthand4 

as a result displayed in a div on the page.

The issues is if I enter "10 10 10" and not enter a fourth number I get a result of

10 10 10 NaN

Can I check for NaN and not get that result processed?

Stuart Robson
  • 1,149
  • 4
  • 22
  • 42

3 Answers3

3

You can check if a value is NaN by using the Javascript isNaN(value) function.

Logan Serman
  • 29,447
  • 27
  • 102
  • 141
2

You could check isNaN() in js.

Note that the isNaN() function only checks for the "numerical nature" of a value, not whether the value it is finite. To check if a number is finite, you should instead use JavaScript's isFinite().

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • Sudhur is right - isFinite detects and returns false on NaN. parseInt returns NaN as you've found. In addition, I suggest you check the length of your numbers array before splitting. If someone enters "10 10 10" the array is missing an element and numbers.length will tell you this. You to assign filler values, return an error to the user, or deal with it in whatever way you see fit. – user8109 Dec 29 '11 at 07:30
1

The isNaN() function determines whether a value is an illegal number (Not-a-Number).

This function returns true if the value is NaN, and false if not.

<script type="text/javascript">
document.write(isNaN(123)+ "<br />");
document.write(isNaN("Hello")+ "<br />");
document.write(isNaN(NaN)+ "<br />");
</script> 

Output:
false
true
true
For your case, you can check like below:

<html>
<body>
<script type="text/javascript">
if(!isNaN(123)){
document.write(isNaN(123)+ "<br />");
}
if(isNaN("Hello")){
document.write(isNaN("Hello")+ "<br />");
}
</script>
</body>
</html>
Chandra Sekhar
  • 16,256
  • 10
  • 67
  • 90
  • does this work if there is either no number or no wording? So if they only enter 10 10 and not 10 10 10 10 so it won't result in 10 10 NaN NaN ? – Stuart Robson Dec 29 '11 at 06:14
  • anyway, in your case **parseInt** will return NaN for empty value. see this [Question](http://stackoverflow.com/questions/6736476/javascript-parseint-return-nan-for-empty-string) – Chandra Sekhar Dec 29 '11 at 06:49