프로형
프로형

Reputation: 27

JS recognizes that var is String

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.

Upvotes: 0

Views: 77

Answers (2)

Pragya Sriharsh
Pragya Sriharsh

Reputation: 559

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.

Upvotes: 3

kasperlauge
kasperlauge

Reputation: 209

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);

Upvotes: 1

Related Questions