Lavinia
Lavinia

Reputation: 263

Function returning NaN when I try to generate basic math operations

I am trying to generate randomly basic math operations(addition, subtractions, multiplication and division) and sometime my function returns NaN. I used function parseInt(), but I still have the same problem. I will appreciate if anybody can help me with any suggestion. Thank you in advance!

Here is my code:

function randNum(min,max)
{
    var num = min+Math.floor((Math.random()*(max-min+1)));
    return num;
}

var choose, operator, firstNum, secondNum,rightAnswer;
function getProb()
{
    var chooseOp=randNum(1,4);
    choose=parseInt(chooseOp);

if (choose==1)
{
    oprator="+";
    var choose1=randNum(0,10);
    var choose2=randNum(0,10);
    firstNum=parseInt(choose1);
    secondNum=parseInt(choose2);
    document.getElementById("mathProb").innerHTML=firstNum+operator+secondNum+"=";
    rightAnswer=choose1 + choose2;
}
else if (choose==2)
{
    operator="-";
    var choose1=randNum(0,10);
    var choose2=randNum(0,10);
    firstNum=parseInt(choose1);
    secondNum=parseInt(choose2);
    document.getElementById("mathProb").innerHTML=firstNum+operator+secondNum+"=";
    rightAnswer=firstNum - secondNum;
}
else if (choose==3)
{
    operator="x";
    var choose1=randNum(0,10);
    var choose2=randNum(0,10);
    firstNum=parseInt(choose1);
    secondNum=parseInt(choose2);
    document.getElementById("mathProb").innerHTML=firstNum+operator+secondNum+"=";
    rightAnswer=choose1 * choose2;
}
    else if (choose==4)
{
    operator="/";
    var choose1=randNum(0,10);
    var choose2=randNum(0,10);
    firstNum=parseInt(choose1);
    secondNum=parseInt(choose2);
    document.getElementById("mathProb").innerHTML=firstNum+operator+secondNum+"=";
    rightAnswer=choose1/choose2;
}
}

Upvotes: 1

Views: 1790

Answers (6)

raina77ow
raina77ow

Reputation: 106453

It's a simple syntax error:

oprator="+"; // should be `operator`

That's why this statement...

firstNum+operator+secondNum+"=";

... will actually be evaluated as ...

firstNum+undefined+secondNum+"=";

The first pair will give you NaN, NaN + Number will be a NaN again, and NaN + String ("=") will result in NaN converted to string, then appended with '=' (hence resulting 'NaN=').

I'd strongly recommend placing "use strict"; line at the beginning of your scripts to catch such errors. With this, you'll get an error:

ReferenceError: assignment to undeclared variable oprator

... and won't need to make SO parse your script for errors instead. )

Sidenotes, I have plenty of them:

  • your randNum function will return you a Number, so no need to use parseInt (you may have to convert arguments of this function, but even that seems not to be necessary here) on its result;

  • if you divide by zero, you get Infinity; if you divide zero by zero, you get NaN as a result; be prepared or adjust the minimums. )

  • you violate DRY principle, repeating most of the statements outputting a result, why don't convert them into a function? Check this snippet (started by @sv_in, completed by me) for example how to do it.

Upvotes: 1

sv_in
sv_in

Reputation: 14049

When choose==1, operator is misspelled as oprator. If you correct it, problem is solved http://jsfiddle.net/uERwd/2/

UPDATE: Your code can be made shorter as: http://jsfiddle.net/uERwd/3/

Upvotes: 5

Denys Séguret
Denys Séguret

Reputation: 382404

Your "NaN" bug is here :

rightAnswer=choose1/choose2;

choose1 an choose2 are integer in [0, 1].

One time over 121, you're dividing 0 by 0, wich gives NaN.

And a little less than one time over 11, you're dividing a not null number by 0, wich gives Infinity.

Upvotes: 2

user1700830
user1700830

Reputation:

When you randomly choose the division operator, it's possible to have zero come out for both choose1 and choose1, which means you attempt to evaluate rightAnswer = 0 / 0;. In Javascript, this equals NaN. Additionally, and this should happen more often, if you choose zero in the denominator any other number in the numerator the answer will come out as Infinity. Of course, zero over anything is zero.

Upvotes: 1

Homr Zodyssey
Homr Zodyssey

Reputation: 768

Your division operation has the possibility of dividing by zero, which would return NaN.

Upvotes: 3

coolxeo
coolxeo

Reputation: 563

You need to specify with a number that represent numeral system, tipically, base 10

http://www.w3schools.com/jsref/jsref_parseint.asp

Add the number 10 to the function call like this

firstNum = parseInt(choose1, 10);

Upvotes: 1

Related Questions