javascript函数无法按预期工作

Clu*_*s92 2 javascript function

我在外部JavaScript文件中有以下代码.我在下面的这一行收到错误: guessNum = inGuess.parseInt(); firebug告诉我parseInt不是一个函数.我认为js中的所有东西基本上都是对象(至少这是我记得在W3School中读到的东西).我确信这很简单,我只是卡住了.欢迎任何建议.谢谢

function inputNum() 
{
/*  initialize variables   */
var inGuess = "";
var loopCt;
var guessResult = "";
var correctNum = 26;
var guessNum = 0;

for (loopCt=1;loopCt<11;loopCt++)
{
    inGuess = prompt("Please enter your guess(enter -1 to exit) Do not press Enter","0");
    if (inGuess == "-1")  { break; }
    if (inGuess==null || inGuess=="")
    {
        alert("Blanks are not allowed.  To exit enter '-1'.");
    } 
    else
    {
        guessNum = inGuess.parseInt(); 
        if (inGuess == "26")
        {
            alert("Congratulations, you guess correctly!");
            guessResult="Correct!";
        }
        else
        if (guessNum < correctNum)
        {
            guessResult="Too low";
        }
        else
        {
            guessResult="Too high";
        }

        document.getElementById('emp'+loopCt).innerHTML=inGuess;
        document.getElementById('ct'+loopCt).innerHTML=guessResult;
    }

}
}   
Run Code Online (Sandbox Code Playgroud)

Bro*_*omb 5

parseInt是一个全球性的职能.您试图从字符串对象访问它,它不存在.

guessNum = parseInt(inGuess, 10); // Tell it what base to use.  Protect against 08 being interpretued as octal.
Run Code Online (Sandbox Code Playgroud)

这将是处理这个问题的正确方法.

parseInt Mozilla开发人员网络文档

  • 脚注 - parseInt可以返回NaNtypeof实际返回相比的内容number