randomgibberish
randomgibberish

Reputation: 35

While loop exits instead of returning to the loop -Javascript

Trying to get only a number answer. The inner while loops exits instead of returning back into the loop. I'm not sure why.

var numPlays = prompt("How many games do you want to play?");
if (isNaN(numPlays) == true) {
    while (isNaN(numPlays) == true) {
        numPlays = prompt("That's not a number. Please pick a number.");
    }
} else if (numPlays == 0) {
    while (numPlays == 0) {
        numPlays = prompt("You have to at least play one game. Please pick a number.");
    }
} else if (numPlays == 1) {
    alert("OK! Let's play " + numPlays + " game!");
} else {
    alert("OK! Let's play best of " + numPlays + "!");
}

Upvotes: 1

Views: 49

Answers (4)

ReactiveRaven
ReactiveRaven

Reputation: 7541

Because of the if/elseif structure; only one of them can match. The first time it runs through, it asks for the number at line 1, then decides which branch of the if/elseif/else structure to go through. It doesn't matter that you're changing the value of numPlays later, the decision was already made!

You probably want a recursive call in here.

function getNumPlays(message) {
    if (!message) { message = "How many games do you want to play?"; }
    var numPlays = parseInt(prompt(message), 10);
    if (isNaN(numPlays)) {
        return getNumPlays("That's not a number.");
    } else if (numPlays < 1) {
        return getNumPlays("You have to at least play one game.");
    }
    return numPlays;
}
var numChosen = getNumPlays();
if (numPlays === 1) {
    alert("OK! Lets play " + numChosen + " game!");
} else {
    alert("OK! Let's play best of " + numChosen + "!");
}

PS: note the parseInt(..., 10) in there. This converts to either a number, or NaN. The '10' part is important; it specifies what base the number is in. Octal numbers are written "011", and sometimes the javascript engine won't know if you want octal or decimal.

Upvotes: 0

Philo
Philo

Reputation: 1989

var numPlays = prompt("How many games do you want to play?");


while (isNaN(numPlays) == true || numPlays == 0) {

        if(isNaN(numPlays) == true)
            numPlays = prompt("That's not a number. Please pick a number.");
        else    
            numPlays = prompt("You have to at least play one game. Please pick a number.");
   }

if (numPlays == 1) 
    alert("OK! Let's play " + numPlays + " game!");
else 
    alert("OK! Let's play best of " + numPlays + "!");

Do we have to have all those nested loops... is that a requirement? can we make it simpler?

Upvotes: 0

Dustin Stiles
Dustin Stiles

Reputation: 1424

There is no need to ever enter a loop.

A quick example

// Wrap in a function so we can start over
function start() {
  var numPlays = prompt("How many games do you want to play?");
  // edited this to check for 0
  if (isNaN(numPlays) || numPlays == 0) {
    alert('Nope, bad answer, try again!');
    return start();
  } else if (numPlays == 1) {
    alert('Ok, lets play 1 game!');
  } else {
    alert('Ok, lets play the best of ' + numPlays + 'games!');
    // A little aside
    // var neededToWin = (Math.floor((numPlays / 2)) + 1);
    // alert('Ok, you need to win ' + neededToWin + ' out of ' + numPlays + ' games to beat me!');
  }
}

Technically you would want the best out of 'half of the games + 1'. So to win, you need to win 7/12 games. You could just find this out by dividing

Upvotes: 1

John Valentine
John Valentine

Reputation: 247

An empty input would evaluate to false ('ok' in your while loop).

Try using parseInt(), which returns NaN for empty strings and non-numbers, Test a few of your own examples using this:

isNaN(parseInt(prompt("enter number")))

Upvotes: 0

Related Questions