1

I'm creating a simple program called guess a number. I'm having problem showing my output. So I don't know if i got the right formula.

<body>
<h3> Guess A Number</br> </h3><input type="integer" id="number" size="20"/>
<input type="button" id="guess" value="GUESS" onClick="guess()"/>

<script>
 function guess() {
 var num = document.getElementById("number").value; 
 var answer = Math.floor(Math.Random() * 100 + 1);

 if(num == answer)
     alert("Your guess is correct! The number is" + answer);

 else if(num != answer) 
     alert("Your guess is incorrect! The number is" + answer);

     }

</script>
</body>
1
  • Open up the Developer Console on your browser. It would have pointed you to the error. Commented Oct 11, 2015 at 13:53

3 Answers 3

2

You had a syntax error when generating a random number.

It should be

Math.random();

But you have

Math.Random();

Here is the complete code:

<body>
<h3> Guess A Number</br> </h3><input type="integer" id="number" size="20"/>
<input type="button" id="guess" value="GUESS" onClick="guess()"/>

<script>
 function guess() {
 var num = document.getElementById("number").value;
 var answer = Math.floor(Math.random() * 100 + 1);

 if(num == answer)
     alert("Your guess is correct! The number is" + answer);

 else if(num != answer)
     alert("Your guess is incorrect! The number is" + answer);

     }

</script>
</body>

When developing Javascript, always have your browser's console turned on. It would have showed you this error. All browsers have consoles.

Sign up to request clarification or add additional context in comments.

1 Comment

Lol thanks, didn't got that! I just started javascript just a few days ago. Thanks
0

Your code is correct but you need to test the function with a fixed number to see if it works, then use Math.random(); (lowercase random()), in this case, I'm using 11 as the correct number..

 function guess() {
 var num = document.getElementById("number").value; 
 //var answer = Math.floor(Math.random() * 100 + 1);
  var answer = 11;

 if(num == answer)
     alert("Your guess is correct! The number is" + answer);

 else if(num != answer) 
     alert("Your guess is incorrect! The number is" + answer);

     }
<body>
<h3> Guess A Number</br> </h3><input type="integer" id="number" size="20"/>
<input type="button" id="guess" value="GUESS" onClick="guess()"/>

</body>

Comments

0

// --- use Math.random instead of Math.Random ------

`

<script>
 function guess() {
 
  var num = document.getElementById("number"); 
  //alert(num);
 var answer = Math.floor(Math.random() * 100 + 1);

 if(num == answer)
     alert("Your guess is correct! The number is" + answer);

 else if(num != answer) 
     alert("Your guess is incorrect! The number is" + answer); 

     }

</script>
<body>
<h3> Guess A Number</br> </h3><input type="integer" id="number" size="20"/>
<input type="button" id="guess" value="GUESS" onClick="guess()"/>
</body>

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.