higuy001 Posted August 5, 2009 Report Share Posted August 5, 2009 Code: function gmn(event) {var myNum = Math.floor(Math.random()*101);changeText("lo", "0");changeText("hi", "100");changeText("ans", myNum);}function changeText(text, withh) {var textFieldToChange = document.getElementById(text);var newTextFieldText = withh; textFieldToChange.value = newTextFieldText;}function getText(text) {var textAreaValue = document.getElementById(text);return parseInt(textAreaValue.value);}function pInt(thing) {return parseInt(thing);}function alerttext(event) {var gess = getText("guess");gess = pInt(gess);var an = getText("ans");an = pInt(an);//NaN detection: \/if (gess == "NaN") {alert("bad");} else {alert(gess);if (gess < an) {changeText("lo", gess);} else if (gess > an) {changeText("hi", gess);}}changeText("guess", "");}function winn() {alert("YouWin!");} For some reason, when I start it, enter a number, and hit guess, I get an alert "NaN" which means that the NaN detection didn't work correctly.Does anyone know why?HiGuy Link to comment Share on other sites More sharing options...
Ingolme Posted August 5, 2009 Report Share Posted August 5, 2009 "NaN" is not a string.If you want to check if something is a number you can use regular expressions: if(/\D/.test(gess)) { alert("bad");} else { Link to comment Share on other sites More sharing options...
higuy001 Posted August 5, 2009 Author Report Share Posted August 5, 2009 Oh. Never thought of that. (Never heard of it either.)But it works!Thanks!HiGuy Link to comment Share on other sites More sharing options...
justsomeguy Posted August 5, 2009 Report Share Posted August 5, 2009 You can also use isNaN. var x = "abc";x = parseInt(x, 10);if (isNaN(x)){ alert("not a number");} Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now