PDA

View Full Version : Prompt problem..

flint0131
06-27-2009, 03:29 PM
Here's my problem..

var enter = prompt("Guess my number? : ", "");
if ( enter == 20 ) {
}
else if ( enter <= 19 ) {
}
else if ( enter >= 21 ) {
}
else {
}
}

Why is that? Everytime I click "X" and Cancel, it won't alert Error!.
Do you have any solution? :confused:

Philip M
06-27-2009, 03:56 PM
<script type = "text/javascript">

var enter = prompt("Guess my number? : ", "");
if (enter != '' && enter != null) { // no text entered or cancel pressed
enter = (parseInt(enter));
if ( enter == 20 ) {
}

else if ( enter <= 19 ) {
}

else if ( enter >= 21 ) {
}

else {
if (isNaN(enter)) {
}
}

}
}

</script>

"In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)

flint0131
06-27-2009, 04:10 PM
Now, It won't alert after clicking "X" or Cancel. :confused:

flint0131
06-27-2009, 04:14 PM
Ok, I got it. :thumbsup: Thanks for the parseInt Idea. Now,
I'm ready for our Laboratory tomorrow. I really thank you.

var enter = parseInt(prompt("Guess my number? : ", ""));
if ( enter == 20 ) {
}
else if ( enter <= 19 ) {
}
else if ( enter >= 21 ) {
}
else {
if (enter = true) {
}
}
}

flint0131
06-27-2009, 04:22 PM
Now, I've got another problem.

var enter = parseInt(prompt("Guess my number? : ", ""));
if ( enter == 20 ) {
}
else if ( enter <= 19 ) {
}
else if ( enter >= 21 ) {
}
else {
if (enter = true) {
}
else if ( !isNaN(enter) ) {
}
}
}

I want to alert Please Enter an Integer! if the User Input is not an integer. Sorry for my annoying questions. :(

Philip M
06-27-2009, 04:50 PM
I don't understand. parseInt() will return an integer so if they enter 20.25 the returned value will be 20 - which is correct.

If you want that only an integer number can be entered in the first place,

var enter = prompt("Guess my number? : ", "");
if (/[^0-9]/gi.test(enter)) { // only digits valid
alert ("You must enter an integer number only");