...

View Full Version : Prompt problem..



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


function ask() {
var enter = prompt("Guess my number? : ", "");
if ( enter == 20 ) {
alert('Correct!');
}
else if ( enter <= 19 ) {
alert('Higher!');
ask()
}
else if ( enter >= 21 ) {
alert('Lower!');
ask()
}
else {
alert("Error!");
ask()
}
}
ask()

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">

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

else if ( enter <= 19 ) {
alert('Higher!');
ask();
}

else if ( enter >= 21 ) {
alert('Lower!');
ask();
}

else {
if (isNaN(enter)) {
alert("Error!");
ask();
}
}

}
}

ask()

</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.



function ask() {
var enter = parseInt(prompt("Guess my number? : ", ""));
if ( enter == 20 ) {
alert('Correct!');
}
else if ( enter <= 19 ) {
alert('Higher!');
ask()
}
else if ( enter >= 21 ) {
alert('Lower!');
ask()
}
else {
if (enter = true) {
alert("Error!");
ask()
}
}
}
ask()

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



function ask() {
var enter = parseInt(prompt("Guess my number? : ", ""));
if ( enter == 20 ) {
alert('Correct!');
}
else if ( enter <= 19 ) {
alert('Higher!');
ask()
}
else if ( enter >= 21 ) {
alert('Lower!');
ask()
}
else {
if (enter = true) {
alert("Trying to esacape eh?");
ask()
}
else if ( !isNaN(enter) ) {
alert("Please Enter an Integer!");
ask()
}
}
}
ask()


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");
ask();
}

I did modify Post#2 to deal with your problem.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum