Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    59
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Prompt problem..

    Here's my problem..
    Code:
    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?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,016
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Code:
    <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)
    Last edited by Philip M; 06-27-2009 at 04:43 PM.

  • #3
    New Coder
    Join Date
    May 2009
    Posts
    59
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Now, It won't alert after clicking "X" or Cancel.

  • #4
    New Coder
    Join Date
    May 2009
    Posts
    59
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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 ) {
    	alert('Correct!');
    	}
    	else if ( enter <= 19 ) {
    	alert('Higher!');
    	ask()
    	}
    	else if ( enter >= 21 ) {
    	alert('Lower!');
    	ask()
    	}
    	else {
        		if (enter = true) {
    		alert("Error!");
    		ask()
    		}
       	}
    }
    ask()

  • #5
    New Coder
    Join Date
    May 2009
    Posts
    59
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Now, I've got another problem.

    Code:
    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.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,016
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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.


  •  

    Posting Permissions

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