www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] How to make prompt repeat if input is Nan

  1. #1
    Join Date
    Apr 2014
    Posts
    44

    resolved [RESOLVED] How to make prompt repeat if input is Nan

    I have this quick code snippet:

    PHP Code:
    var sQuantity 0
    if (sQuantity || isNaN(sQuantity)) {
            (
    sQuantity parseInt(prompt("Let's begin compiling our student database. How many students would you like to enter?""Number of Students")));
    (
    sQuantity 1);

    But when I run it like this, the prompt doesn't come up at all. Obviously I'm mistaken how this should work. How do I get a prompt to keep popping up if an Integer is not entered? I'm probably going about this all wrong...

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    1,033
    Well with the code you have now, there isn't really a loop. Plus you set sQuantity at the end of your if() statement so it wouldn't be able to repeat in that sense either.

    A while() loop would probably work best in this case since you want it to repeat until a certain condition is met. However I will advise caution as while() loops can be come a nuisance if the condition is not set properly.

    You could try something like this:
    Code:
    var sQuantity;
    while(isNaN(sQuantity)) {
    	sQuantity = parseInt(prompt("Let's begin compiling our student database. How many students would you like to enter?", "Number of Students"));
    }
    Alternatively you could add a condition to the while() loop if you want a value greater than 0 to be given.
    Code:
    var sQuantity;
    while(isNaN(sQuantity) || sQuantity <= 0) {
    	sQuantity = parseInt(prompt("Let's begin compiling our student database. How many students would you like to enter?", "Number of Students"));
    }

  3. #3
    Join Date
    Apr 2014
    Posts
    44
    Awesome! The while() loop works wonders. Thanks again!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles