我尝试生成基本数学运算时返回NaN的函数

Lav*_*nia 1 javascript

我试图生成随机基本的数学运算(加法,减法,乘法和除法),有时我的函数返回NaN.我使用了函数parseInt(),但我仍然遇到同样的问题.如果有人可以帮我提出任何建议,我将不胜感激.先感谢您!

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;
}
}
Run Code Online (Sandbox Code Playgroud)

sv_*_*_in 5

choose==1,operator拼写错误的oprator.如果你纠正它,问题就解决了 http://jsfiddle.net/uERwd/2/

更新: 您的代码可以缩短为:http://jsfiddle.net/uERwd/3/