Can't run my function multiple times and break my code.

I am trying to run this feature until the player or computer has won 5 times. Without the while loop, it works once and everything works fine. As soon as I add the while loop, the function still only runs once and returns undefined to me.

function playToFive() {
  console.log('Let\ 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());
      

Run codeHide result


+3


source to share


5 answers


while(playerWins === 5 || computerWins === 5)

      

Your while loop will never execute as you are checking for equality, and both playerWins

are and computerWins

are 0

initially.

You may be looking for a more appropriate condition:

while(playerWins < 5 && computerWins < 5)

      

Note that we are using logical AND &&

instead of logical OR ||

. This is because you don't want to keep looping until they both win. Logical OR means that even if the computer won, but the player does not have it, we will continue the loop. Only one of the conditions must be true for the entire statement to be true.



When we use logical AND, if one of them is false (meaning that if only one player has already achieved 5 wins), then we will exit 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 have not been achieved yet, it will return an array of the player's wins and beat the computer.

You have to put a return statement after the loop to start the loop 5 times and then return after someone has won.

Finally, since you did not provide the rest of the code, I am not sure if it is actually humanVsMachine

defined; if you've identified what's outside of the function then you're good to go.

+3


source


After the if-else structure is evaluated and executed, the statement return

is called unconditionally, thereby ending the loop prematurely. Instead, you should place return

after . In addition, the loop condition must be until none of the players reaches 5 wines (estimated with the operator <

):



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

      

+2


source


You will need to move the statement return

behind the while loop, but you will also need to change the conditions in your loop while

- now it only fires if playerWins

or computerWins

exactly 5 when in fact it needs to stop running at that point (sic while(playerWins < 5 && computerWins < 5)

)

+1


source


Return will complete the cycle

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

alert(returnMe()); Try this out 


function playToFive() {
    console.log('Let\ 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());

      

0


source


You return inside a while loop, which ends the loop. Therefore, move the return code outside of the loop as shown below. Also note that your boolean operator will prevent the loop from starting correctly.

function playToFive() {
    console.log('Let\ 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());

      

Let me add that spacing your code helps a lot, it looks a little "vertical" with closing parentheses. Please note that the spacing between the above helps to visually identify such things :)

0


source







All Articles