0

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?

5
  • You can nest functions with javascript??? wtf? Commented Sep 17, 2013 at 18:31
  • @progenhard: Yes, you can. Learn about closures. Commented Sep 17, 2013 at 18:31
  • @SLaks I've never done work with javascript so it's an entire field i need to learn about <.< Commented Sep 17, 2013 at 18:32
  • Disclaimer: I'm an idiot...you are quite modest!!! Commented Sep 17, 2013 at 18:35
  • Thanks everyone for all the responses! They're very much appreciated! Now, I'm off to research the difference between parseInt() vs. Number() Commented Sep 17, 2013 at 18:51

5 Answers 5

2

May need to use parseInt to convert arguments into numbers:

this.goalAmount = parseInt(goalAmount);
this.currentStake = parseInt(currentStake);
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

2 Comments

For some reason, when I tried this, it fixed the issue but created an issue when I tried dividing the currentStake by the goalAmount to get a percentage. There was no error, but it was returning unexpected results. When I used the parseInt() function all was copacetic.
0

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

Comments

0

change this line

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

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.