-1
<body>
<input id ="guessNumber" type="text">
<button id="button" onclick="check()">click here</button>



<script language="javascript" type="text/javascript">
var randomNumber = Math.floor((Math.random()*6));
var guessNumber = document.getElementById("guessNumber").value;

function check() {
    if(guessNumber==randomNumber){
        alert("you are correct");
    }
    else {
        alert("you are wrong");
    }
}
</script>
</body>

I want to write a small number game program where I put in a number and if the Math.random method value are equal it should alert "TRUE" but its just alerting "FALSE"("it is wrong" my alert code) all the time.Give me an explanation why that is happening not just the answer and why is guessNumber variable a string ?

am guru prasath
  • 345
  • 1
  • 7
  • 28

2 Answers2

4

This line is outside of the function.

var guessNumber = document.getElementById("guessNumber").value;

It gets the value once, it does not update as the text changes. Move it inside the method.

Second issue, you are comparing a number and a string. The value of an input is a string. You are trying to compare a number and a string. Type console.log("3"===3); into your console.

var randomNumber = Math.floor((Math.random() * 6));

function check() {

  var guessNumber = parseInt(document.getElementById("guessNumber").value, 10);
  if (guessNumber === randomNumber) {
    alert("you are correct");
  } else {
    alert("you are wrong");
  }
}
<input id="guessNumber" type="text">
<button id="button" onclick="check()">click here</button>

Maybe if you run a test like this you will understand

var randomNumber = Math.floor((Math.random() * 6));

var initialValue = document.getElementById("guessNumber").value;
var currentValue;

function check() {

  currentValue = document.getElementById("guessNumber").value;
  alert("initialValue: " + initialValue + " | currentValue: " + currentValue + " | randomNumber : " + randomNumber);
  
  var guessNumber = parseInt(currentValue, 10);
  if (guessNumber === randomNumber) {
    alert("you are correct");
  } else {
    alert("you are wrong");
  }
}
<input id="guessNumber" type="text" value="INITIAL VALUE">
<button id="button" onclick="check()">click here</button>
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • Ya guessNumber is a global object right it can update ? – am guru prasath Apr 03 '15 at 14:44
  • The variable is global, BUT the value will not automatically get updated. It will be whatever it is when the code first runs. – epascarello Apr 03 '15 at 14:44
  • Ya but I am just reading it not updating that part,I am updating just a window object alert. – am guru prasath Apr 03 '15 at 14:47
  • Like if I am right from what you say a function can read a global object but not update it ? or is it that the variable I am calling is inside function and inside if else> ? Would it work if it was still in If else or function. – am guru prasath Apr 03 '15 at 14:49
  • You are assigning the value to the variable, you are NOT assigning a reference to the input's property. It stores the value at that point in time. When you update the input to a new value, the variable is NOT updated. You need to read the value again. – epascarello Apr 03 '15 at 14:49
  • The function IS reading the global variable. The fact is the variable DOES NOT get updated. It stays the same exact value as it was set since you never read the value again. – epascarello Apr 03 '15 at 14:51
  • The getNumber value stores the value.Later it is accessed by the if statement to state if its true or not.Thats its stored and accessed it dosent update ?I am not getting what you are saying please use less technical terms. – am guru prasath Apr 03 '15 at 15:14
  • Did you run the code!? When you read the value, it is like taking a photo. It is a snapshot of that time. When you look at the photo it does not update to the new things that entered into that frame. If you want to see the new items in that photo, you need to take a new picture to see them. So you take another picture. – epascarello Apr 03 '15 at 15:16
  • your method worked,I dont know why the variable should stay inside,it is accessed by if right ?the value is stored right ?Then what are you taking about updating ? etc etc ? – am guru prasath Apr 03 '15 at 15:19
  • Here is what I understand.See the getNumber stores the variable before I input my data(i put a number in the box) but the function runs if I type the number and click on the button at that time the variable is not getting updated of the value I entered ? – am guru prasath Apr 03 '15 at 15:24
  • Yes because the string stored in the variable only holds the value when the line was run. It is not auto updated to the newly entered value so you need to do that check inside the function. I updated the answer above with code that hopefully shows that. – epascarello Apr 03 '15 at 15:26
  • I know THANK YOU should not be used as a comment in SO,but I will do so if i din't do it its a shame for me.THANK YOU sir I really respect you patience for helping a dumb stranger.May God Bless you..My wishes :) – am guru prasath Apr 03 '15 at 15:30
  • Glad I could help. I was a beginner a long time ago. :) – epascarello Apr 03 '15 at 15:31
-1

guessNumber is a string and randomNumber is a number. Try this:

function check() {
    if(guessNumber==randomNumber){
        alert("you are correct");
    }
    else {
        alert("you are wrong");
    }
}

https://stackoverflow.com/a/359509/2690289

Community
  • 1
  • 1
Geoffrey Burdett
  • 1,906
  • 1
  • 14
  • 26