Can't get my function to run more than once and break my code

255 views Asked by At

I'm trying to run this function until the player or the computer wins 5 times. Without the while loop, it runs one time and everything works fine. As soon as I add a while loop, the function still runs only once and it gives me an undefined return.

function playToFive() {
  console.log('Let\'s play Rock Paper Scissors');
  var playerWins = 0;
  var computerWins = 0;
  while (playerWins === 5 || computerWins === 5) {
    if (humanVsMachine === 'player') {
      playerWins += 1;
    } else if (humanVsMachine === 'computer') {
      computerWins += 1;
    }
    return [playerWins, computerWins];
  }
}

console.log(playToFive());

5

There are 5 answers

1
Purag On BEST ANSWER
while(playerWins === 5 || computerWins === 5)

Your while loop will actually never execute, since you're checking for equality and both playerWins and computerWins are 0 initially.

You may be looking for a condition more like this:

while(playerWins < 5 && computerWins < 5)

Note that we're using the logical AND && instead of the logical OR ||. This is because you don't want to keep looping until both of them win. The logical OR means that even if the computer has won but the player hasn't, we'll continue looping. Only one of the conditions needs to be true for the whole statement to be true.

When we use the logical AND, if one of them is false (meaning, if only one player has reached 5 wins already), then we will break out of the loop as we should.

The next problem is that you have a return statement in your while loop, so after the first execution, even if 5 wins haven't been reached yet, it will return the array of the player's wins and the computer's wins.

You should put the return statement after the loop so that you run the loop 5 times and then return after somebody has won.

And finally, since you haven't provided the rest of the code, I'm not sure if humanVsMachine is actually defined; if you defined that outside of the function then you're good to go.

1
Ritesh  Karwa On

Return will terminate your loop

function returnMe() {
  for (var i=0; i<2; i++) {
    if (i === 1) return i;
  }
}

alert(returnMe()); Try this out 


function playToFive() {
    console.log('Let\'s play Rock Paper Scissors');
    var playerWins = 0;
    var computerWins = 0;
    while (playerWins === 5 || computerWins === 5) {
        if (humanVsMachine === 'player') {
        playerWins += 1;
        } else if (humanVsMachine === 'computer') {
        computerWins += 1;
        }
    // return [playerWins, computerWins]; moving code to below
    }
    return [playerWins, computerWins]; // moved return outside the loop here
}

console.log(playToFive());
3
AGE On

You're returning inside the while loop, which will end the looping process. Therefore move your return code outside the loop like below. Also notice that your logical statement is not going to allow the loop to run properly.

function playToFive() {
    console.log('Let\'s play Rock Paper Scissors');
    var playerWins = 0;
    var computerWins = 0;
    while (playerWins < 5 && computerWins < 5 ) {
        if (humanVsMachine === 'player') {
        playerWins += 1;
        } else if (humanVsMachine === 'computer') {
        computerWins += 1;
        }
    // return [playerWins, computerWins]; moving code to below
    }
    return [playerWins, computerWins]; // moved return outside the loop here
}

console.log(playToFive());

Allow me to add that spacing your code helps a lot, it's a little bit 'vertical' looking with the closing brackets. Note how spacing on the above helps to visually identify things like this :)

0
Psychemaster On

You'll need to move the return statement outside the while loop, but you'll also need to change your conditions on your while loop - right now it only runs if either playerWins or computerWins are exactly 5, when in fact it needs to stop running at that point (so while(playerWins < 5 && computerWins < 5))

0
Mureinik On

After the if-else structure is evaluated and executed, a return statement is called unconditionally, thus terminating the loop prematurely. You should place the return after the loop instead. Additionally, the loop condition should be as long as neither player reaches 5 wines (evaluated with the < operator):

while (playerWins < 5 && computerWins < 5 ) {
    if (humanVsMachine === 'player') {
        playerWins +=1;
    } else if (humanVsMachine === 'computer') {
        computerWins += 1;
    }
}
return [playerWins, computerWins];