Whats the problem


#1

var num = prompt("ENTER ANY THING."); // i entered 45
if(Number.isInteger(num) ){
alert("NUMBER");
}
else{
alert("NOT A NUMBER"); // output was "NOT A NUMBER".
}

what is wrong, please write a full code...


#2

guess what? We can use the typeof function to see the type:

var num = prompt("ENTER ANY THING."); // i entered 45
console.log(typeof(num));
if(Number.isInteger(num) ){
alert("NUMBER");
}
else{
alert("NOT A NUMBER"); // output was "NOT A NUMBER".
}

oh dear, it is a string. It seems prompt stores the number as string. In fact, prompt stores everything as string. You would need to see if it could be casted to integer.


#3

Then how can i do this with integer...


#4

you need to convert the string to a integer, there are two build in functions which you can use, parseInt():

var num = prompt("ENTER ANY THING."); 
num = parseInt(num);

careful, parse int will convert "42 years" into 42. (years 42 would be NaN) The other options filterInt:

var num = prompt("ENTER ANY THING."); 
num = parseInt(num);

this will only get you the number if there are no other characters, otherwise returns NaN. NaN stands for: Not a Number


#5

Thanx, and sorry for my unknowing. feeling illitrate


#6

var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damagedThisRound = Math.floor(Math.random() * 5 + 1);
var totalDamage = 0;

while (slaying){
if(youHit){
alert("YOU HIT THE DRAGON AND DID " + damagedThisRound + "DAMAGE !");
totalDamage += damagedThisRound;
if(totalDamage >= 4){
alert("YOU DID IT ! YOU SLOW THE DRAGON !");
slaying = false;
}else{
youHit = Math.floor(Math.random() * 2);
}
{
else{ // unexpected token else
alert("THE DRAGON BURINATES YOU ! YOU'RE TOAST !");
}
slaying = false;
}
}


#7

{ // what is the { doing here? It doesn't belong. 
 //Shouldn't it be a }? Take a close look at all the brackets
	else{  // unexpected token else

see comment code