Reputation: 103
so I am doing a function which supposed to make a block of 10 numbers, but in case if it is a string, prompt "can't be string" with window.alert.
So I am making a test with console.log to check if the value isNaN or not. It always returns true, but if I am doing the same thing in browser it is false, this is what makes me struggling. Kindly thank you for sharing your wisdom with me !
I was looking for the answer without success. Best regards.
btnCheck.addEventListener("click", divFunction);
function divFunction(){
var number = Number(document.getElementById("number").value);
console.log(isNaN(number.value)); //Why it always print true ?
if(isNaN(number.value) === "true"){window.alert("CAN'T BE STRING")}else{
document.getElementById("myDiv").innerHTML="";
var i;
for (i=0;i<10;i++){
document.getElementById("myDiv").innerHTML+=number+"</br>";
}
}
}
<input type="text" id="number" placeholder="give me number"></input>
</br>
<button id="buttonCheck">Cyc</button>
</br>
<div id="myDiv"></div>
Upvotes: 0
Views: 31
Reputation: 3675
You need to remove the .value
part. Do this instead:
isNaN(number)
Upvotes: 1