1

I have the following code in an external javascript file. I am getting an error on this line below: guessNum = inGuess.parseInt(); firebug tells me the parseInt is not a function. I thought all things in js were basically objects (at least that is what I remember reading in W3School). I am sure this is something simple, I am just stuck. Any suggestions are welcome. Thanks

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

}
}   
1
  • 4
    Use MDN for docs, not W3Schools. Commented Sep 17, 2013 at 16:26

5 Answers 5

5

parseInt is a global function. You are trying to access it off of a string object, where it doesn't exist.

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

That would be the correct way to handle this.

parseInt Mozilla Developer Network Docs

  • Footnote - parseInt can return NaN which when compared with typeof actually returns number
Sign up to request clarification or add additional context in comments.

Comments

2

parseInt is a method on window, not on a string. You want

guessNum = parseInt(inGuess, 10);

The second argument insures that your code will treat the first argument as a base-10 number, meaning it will correctly parse "010" as 10 and reject "0x10" instead of parsing it as 16.

I thought all things in js were basically objects

They are objects, but that doesn't mean that all objects have the same set of methods defined on them.

Comments

1

If you do want to use it like that for whatever exotic reason, you can define prototype on the String object:

String.prototype.parseInt = function() {

    return parseInt(this,10);
}

var inGuess = "26";

alert(inGuess.parseInt());

1 Comment

Ah, durr, yeah. Sorry :p
0

Your syntax isn't quite right... From the console:

> x = parseInt("2", 10)
2

Also, something to keep in mind, which come from the docs...

If the input string begins with "0", radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.

parseInt() Documentation

Comments

0

inGuess is a string and string does not have parseInt function. parseInt is a global function.

do this:

guessNum = parseInt(inGuess); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.