jgrant
jgrant

Reputation: 659

javascript comparison operator not evaluating user input correctly

Disclaimer: I'm an idiot.

This is extremely simple. I must be doing something very basic wrong, but I've spent a ton of time trying to figure it out, and I'm stumped.

For reference, The Entire Code:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Not comparing correctly</title>
</head>
<script>

//This is the main function. It creates a new game and plays it.
function playTheGame() {
    "use strict";
    //create the game object
    var currentGame = new game();
    //play the game
    currentGame.play(document.getElementById("eCurrentStake").value, document.getElementById("eGoalAmount").value);
}

// This is the game object.
function game(){
    //properties
    this.goalAmount = 0;
    this.currentStake = 0;

    //Play method Plays the game and stores the results in properties
    this.play=play;
    function play(currentStake,goalAmount){
        //set the relevant properties to values passed to the object
        this.goalAmount = goalAmount;
        this.currentStake = currentStake;
        alert(this.currentStake+" < "+this.goalAmount+" :"+(this.currentStake < this.goalAmount));
    };
}
</script>

<body>
    Enter Current Stake: <input type="text" id="eCurrentStake"></input><br>
    Enter Goal Amount: <input type="text" id="eGoalAmount"></input><br>

    <button type="button" onclick="playTheGame()">Let's Play!</button>
</body>
</html>

The part of the code that's giving me trouble:

alert(this.currentStake+" < "+this.goalAmount+" :"+(this.currentStake < this.goalAmount));

When I put 9 and 10 into the input text fields, I get a, "false," output. but 10 and 50 does return true. In short: the result is unpredictable. I think it might be evaluating as a string, but when I researched it, all I can find is that Javascript has dynamic data types and will compare variables as numbers if they are numbers. ie :

http://www.w3schools.com/js/js_datatypes.asp

So, where am I going wrong?

Upvotes: 0

Views: 143

Answers (5)

Tony
Tony

Reputation: 17637

change this line

currentGame.play(parseInt(document.getElementById("eCurrentStake").value), parseInt(document.getElementById("eGoalAmount").value));

Upvotes: 0

Rusty Bailey
Rusty Bailey

Reputation: 131

May need to use parseInt to convert arguments into numbers:

this.goalAmount = parseInt(goalAmount);
this.currentStake = parseInt(currentStake);

Upvotes: 2

user2625787
user2625787

Reputation:

Input values are indeed strings, as you suspect. Cast them to numbers:

this.goalAmount = Number(goalAmount);
this.currentStake = Number(currentStake);

It boils down to this:

"9" < "10" // false
Number("9") < Number("10") // true

Upvotes: 0

Jaime
Jaime

Reputation: 1

You can use the number function: http://www.w3schools.com/jsref/jsref_number.asp

Upvotes: 0

Billy Moon
Billy Moon

Reputation: 58521

wrap the things you want to compare with parseInt to convert them to numbers - otherwise they will be compared as strings

Upvotes: 0

Related Questions