Clueless92
Clueless92

Reputation: 29

javascript function not working as expected

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

}
}   

Upvotes: 1

Views: 73

Answers (5)

user229044
user229044

Reputation: 239260

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.

Upvotes: 2

suff trek
suff trek

Reputation: 39777

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());

Upvotes: 1

Brombomb
Brombomb

Reputation: 7076

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

Upvotes: 5

gp.
gp.

Reputation: 8225

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

do this:

guessNum = parseInt(inGuess); 

Upvotes: 0

brbcoding
brbcoding

Reputation: 13586

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

Upvotes: 0

Related Questions