Ending while loop in Console version of rock, paper, scissors


Hi Everyone,

I was hoping someone could take a look at my code for the rock paper scissors exercise. I think I’ve met all requirements of the exercise except ending the game when either the cpu or human reach 5 wins. I’ve set up a while look with the instructions to end the game when someone reaches 5 wins, but it just keeps going. I’ve looked at different articles to make sure I’m constructing the while loop appropriately. I’ve also tried putting the while loop in different places but I haven’t had any luck. I was hoping to get a nudge in the right direction. My code is below:

function game() {

    let userScore = 0;
    let cpuScore = 0;
    // loop to run game until user or cpu reach 5
    while (userScore || cpuScore < 5) {

    function oneRound() {

        function computerPlay() {

            // define random numbers and their assignments
            console.log("1 = rock \n2 = paper \n3 = scissors");

            // Generate random number
            let randomNumber = Math.floor((Math.random() * 3) + 1);
            console.log("This round's random number is: " + randomNumber + ".");
            // Assign rock, paper, or scissors based on random number
            if (randomNumber === 1)  {
                return 'rock';
            } else if (randomNumber === 2) {
                return 'paper';
            } else {
                return 'scissors';

        //console.log("The computer has played: " + computerPlay() + ".");

        function playRound(userSelection,cpuSelection) {

            // round outcomes
            if (userSelection === "rock") {
                if (cpuSelection === "paper") {
                    return "Paper beats rock. You lose!";
                } else if (cpuSelection === "scissors") {
                    return "Rock beats scissors. You win!";
                } else if (userSelection === cpuSelection) {
                    return "Rock can't beat rock. This is a draw.";

            } else if (userSelection === "paper") {
                if (cpuSelection === "rock") {
                    return "Paper beats rock. You win!"
                } else if (cpuSelection === "scissors") {
                    return "Scissors beats paper. You lose!"
                } else if (userSelection === cpuSelection) {
                    return "Paper can't beat paper. This is a draw."

            } else if (userSelection === "scissors") {
                if (cpuSelection === "rock") {
                    return "Rock beats scissors. You lose!"
                } else if (cpuSelection === "paper") {
                    return "Scissors beats paper. You win!"
                } else if (userSelection === cpuSelection) {
                    return "Scissors can't beat scissors. This is a draw."


        const userSelection = prompt("Enter: Rock, Paper, or Scissors.").toLowerCase();
        console.log("The human has selected: " + userSelection + ".");

        const cpuSelection = computerPlay();
        console.log("The computer has selected: " + cpuSelection + ".");


        console.log("The human score is: " + userScore + ".");
        console.log("The computer score is: " + cpuScore + ".");





You can’t shortcut while loops like that in JS unfortunately.

You will need to do while (userScore < 5 || cpuScore < 5)


Gotcha. I made that adjustment. Weird thing is that the loop still continues after either score reaching 5. I’m doing a console.log for the score so I know that the score is indeed being tracked.

Checking out google to see if someone has had a similar issue.


Actually tested it for a bit after 5 wins for a user. It turns out that my code does stop the program. But it only stops when both users reach 5 points.


use && instead of || and the issue should be fixed.


Cool. So that fixed it. but I guess I’m a little confused.

My understanding was that with | | , the function would run until the cpuscore or userscore reached five. Once one of those reached five, I expected it to end.

With the &&, it is behaving exactly the way I wanted. My impression of && is that it would make it so that the function would run until both the cpu score and userscore reached five.

I’m checking out some more resources online. I guess I am just not understanding what those two do.

I really appreciate your tips. Thank you!


Hi @bycdiaz

You have it the wrong way round. With || only one side needs to be true for the loop to continue. So while either playerScore or cpuScore was less than 5 the loop would continue. With && both sides must be true so as soon as either side becomes false (i.e. when one of the scores goes to 5) the loop breaks.

This kind of issue is known as a logic error and is annoying hard to spot because the code is correct so isn’t caught by the debugger, it’s just the logic that is wrong.