最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Function returning NaN when I try to generate basic math operations - Stack Overflow

programmeradmin1浏览0评论

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;
}
}

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;
}
}
Share Improve this question asked Sep 27, 2012 at 15:03 LaviniaLavinia 2631 gold badge3 silver badges16 bronze badges 10
  • 7 Never use parseInt without specifying the radix. – Denys Séguret Commented Sep 27, 2012 at 15:04
  • 1 I'd also suggest to use a switch construct to avoid all those if (choose==. – Denys Séguret Commented Sep 27, 2012 at 15:07
  • 3 There's no need to use parseInt() here at all. – Pointy Commented Sep 27, 2012 at 15:07
  • 2 choose1/choose2 will sometimes be Infinity. – Denys Séguret Commented Sep 27, 2012 at 15:12
  • 2 @Shusl choose2 is an integer in [0, 10]. – Denys Séguret Commented Sep 27, 2012 at 15:18
 |  Show 5 more ments

6 Answers 6

Reset to default 5

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

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

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

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.

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

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

Add the number 10 to the function call like this

firstNum = parseInt(choose1, 10);

When you randomly choose the division operator, it's possible to have zero e 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 e out as Infinity. Of course, zero over anything is zero.

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 remend 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, pleted by me) for example how to do it.

发布评论

评论列表(0)

  1. 暂无评论