Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Prompt problem..

1. ## Prompt problem..

Here's my problem..
Code:
```function ask() {
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?

• Code:
```<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)

• Now, It won't alert after clicking "X" or Cancel.

• Ok, I got it. Thanks for the parseInt Idea. Now,
I'm ready for our Laboratory tomorrow. I really thank you.

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

• Now, I've got another problem.

Code:
```function ask() {
var enter = parseInt(prompt("Guess my number? : ", ""));
if ( enter == 20 ) {
}
else if ( enter <= 19 ) {
}
else if ( enter >= 21 ) {
}
else {
if (enter = true) {
alert("Trying to esacape eh?");
}
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.

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

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

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•