0

I think the JS thought that var num was a String, but I want var num as a number! The problem says 'num' is not defined.

var num = document.getElementById('number').value;
var i = Math.floor(Math.random(0,num));
var c = document.getElementById('chance').value;
 function clicked(){
 var j = document.getElementById('game').value;
 if (i > j){
 document.getElementById('demo').innerHTML = "Bigger!";
 c -= 1;
 document.getElementById('re').innerHTML = "You have " + c + " more chances!";
 } else if(num < j){
 document.getElementById('demo').innerHTML = "The value is bigger than " + num + "!";
 document.getElementById('re').innerHTML = " "
 } else if (i < j){
 document.getElementById('demo').innerHTML = "Smaller!";
 c -= 1;
 document.getElementById('re').innerHTML = "You have " + c + " more chances";
 } else if (i == j){
 document.getElementById('demo').innerHTML = "Correct!";
 c = 14;
 document.getElementById('re').innerHTML = " ";
 i = Math.floor(Math.random()*num);
 }
 if (c == 0){
 document.getElementById('demo').innerHTML = "Try again! The secret number was " + num;
 c = 14;
 document.getElementById('re').innerHTML = " ";
 i = Math.floor(Math.random()*101);
 }
 }
<p>
 You should guess a number between 0 ~ <input type="number" id="number"> under this text. You have total <input type="number" id="chance"> chances. Good luck!<br><br>
 <input type="number" id="game"> <input type="submit" onclick="clicked()" value="GUESSED">
 <p id="demo"></p>
 <p id="re"></p>
</p>

I want an ID 'number' be var num, the random number between 0 and num: which will be the var I, and an ID 'chance' be var c, the number of chances be the c. I hope you know what I mean, because I described precisely as I can. By the way, I am coding a number guessing game HTML and I am a coding nooooob. If you are nice, please help me out. Thank You.

Keith
24.4k2 gold badges32 silver badges48 bronze badges
asked Apr 27, 2019 at 20:21
1

2 Answers 2

3

See doc : https://gomakethings.com/converting-strings-to-numbers-with-vanilla-javascript/

 var num1 = parseInt(num, 10);

The parseInt() method converts a string into an integer (a whole number).

It accepts two arguments. The first argument is the string to convert. The second argument is called the radix. This is the base number used in mathematical systems. For our use, it should always be 10.

The Number() method converts a string to a number.

Sometimes it’s an integer. Other times it’s a point number. And if you pass in a string with random text in it, you’ll get NaN, an acronym for "Not a Number."

As a result of this inconsistency, it’s a less safe choice than parseInt() and parseFloat(). If you know the format of the number you’d like, use those instead. If you want the string to fail with NaN if it has other characters in it, Number() may actually be a better choice.

answered Apr 27, 2019 at 20:31
Sign up to request clarification or add additional context in comments.

Comments

1

var num = document.getElementById('number').value always returns a string value. You can cast the value using https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number

num = Number(num);
answered Apr 27, 2019 at 20:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.