Rock, paper, scissors project - Can't get computer's choice to log, or anything really

Hi everyone,

So I’m busy going through the Rock, Paper, Scissors project. It’s working okay, I think.
However, I want to be able to log the computer’s choice, so it would look something like:
console.log("The computer chose " + choices);
Thing is, nothing seems to log, unless I put it outside the playRound function. However, that causes me to run into another problem where the console only logs that message the first time.
Here’s the full code:

let choices = ["rock", "paper", "scissors"]

function computerPlay() {
    let computerSelection = Math.floor(Math.random() * choices.length);
    return choices[computerSelection].toLowerCase();
}

let computerSelection = computerPlay();

function playRound() {
    computerPlay();
    let playerSelection = prompt("Rock, paper, or scissors?").toLowerCase()

    if (playerSelection == computerSelection) {
        return "Draw! Try again!";
    }

    else if (playerSelection == "rock" && computerSelection == "scissors") {
        return "You win! Nice one!";
    }

    else if (playerSelection == "rock" && computerSelection == "paper") {
        return "You lose! Ouch!";
    }

    else if (playerSelection == "paper" && computerSelection == "rock") {
        return "You win! Nice one!";
    }

    else if (playerSelection == "paper" && computerSelection == "scissors") {
        return "You lose! Ouch!";
    }


    else if (playerSelection == "scissors" && computerSelection == "rock") {
        return "You lose! Ouch!";
    }

    else if (playerSelection == "scissors" && computerSelection == "paper") {
        return "You win! Nice one!";
    }

    else {
        return "What was that? Try again...";
    }
    console.log("The computer chose " + computerSelection + ". " + playRound());
    }

    playRound();

Hello there! If I’m looking at this correctly (a huge if as I’m around that same lesson as well) I believe you’re problem is where your (console.log("the computer chose…) phrase is located. If you locate it under your computerPlay() function and remove playRound(), I think that should do the trick. We actually have a very similar answer to this problem. This is what I have for my playRound function.

function playRound(playerSelection, computerSelection){
    playerSelection = window.prompt("Rock, Paper, Scissors...Shoot!")
    playerSelection = playerSelection.toLowerCase()
    console.log("The player has selected " + playerSelection)
    computerSelection = computerPlay()
    console.log("The computer has selected " + computerSelection)

    if(playerSelection === "rock" && computerSelection === "paper"){
        finalAnswer = "You Lose!  Paper covers rock"
        score = 0
    }else if(playerSelection === "paper" && computerSelection === "rock"){
        finalAnswer = "You Win! Paper covers rock"
        score = 1
    }else if(playerSelection === "scissors" && computerSelection === "rock"){
        finalAnswer = "You Lose! Rock crushes scissors"
        score = 0
    }else if(playerSelection === "rock" && computerSelection === "scissors"){
        finalAnswer = "You Win! Rock crushes scissors"
        score = 1
    }else if(playerSelection === "scissors" && computerSelection === "paper"){
        finalAnswer = "You Win! Scissors cuts paper"
        score = 1
    }else if(playerSelection === "paper" && computerSelection === "scissors"){
        finalAnswer = "You Lose! Scissors cuts paper"
        score = 0
    }else{
        finalAnswer = "It is a tie!"
        score = 2
    }

    console.log(finalAnswer)
    console.log(score)
    return score

I got it, thank you! However, I am having even more trouble.
I cannot get the game to repeat itself until the number of rounds reaches 5. I don’t want to use a loop. The project page makes this sound possible, by saying

You have not officially learned how to “loop” over code to repeat function calls… if you already know about loops from somewhere else (or if you feel like doing some more learning) feel free to use them. If not, don’t worry! Just call your playRound function 5 times in a row. Loops are covered in the next lesson.

However, when I try, it doesn’t work. Here’s my code.

let choices = ["rock", "paper", "scissors"]

//Lets the computer make a choice
function computerPlay() {
    let computerSelection = Math.floor(Math.random() * choices.length);
    return choices[computerSelection].toLowerCase();
}

let computerSelection = computerPlay();
let round = 1;
let playerScore = 0;
let computerScore = 0;

//Play a single round
function playRound(playerSelection, computerSelection) {
        
        if (playerSelection == computerSelection) {
            round++;
            return "Draw! Try again!";
        }

        else if (playerSelection == "rock" && computerSelection == "scissors") {
            playerScore++;
            round++;
            return "You win! Nice one!";
        }

        else if (playerSelection == "rock" && computerSelection == "paper") {
            computerScore++;
            round++;
            return "You lose! Ouch!";            
        }

        else if (playerSelection == "paper" && computerSelection == "rock") {
            playerScore++;
            round++;
            return "You win! Nice one!";            
        }

        else if (playerSelection == "paper" && computerSelection == "scissors") {
            computerScore++;
            round++;
            return "You lose! Ouch!";
        }


        else if (playerSelection == "scissors" && computerSelection == "rock") {
            computerScore++;
            round++;
            return "You lose! Ouch!";
        }

        else if (playerSelection == "scissors" && computerSelection == "paper") {
            playerScore++;
            round++;
            return "You win! Nice one!";
        }

        else {
            round--;
            return "What was that? Try again...";
        }
    }

    //Play a 5-round game
    function game() {            

        if (round <= 5) { 
            let playerSelection = prompt("Rock, paper, or scissors?").toLowerCase();
            computerPlay();
            console.log("You have chosen " + playerSelection);
            console.log("PC has chosen " + computerSelection);
            console.log("Round: " + round);
            console.log(playRound(playerSelection, computerSelection));
            console.log("Score: " + playerScore);
            console.log("PC Score: " + computerScore);
        }

        else if (playerScore > computerScore) {
            console.log("Score: " + playerScore);
            console.log("PC Score: " + computerScore);
            console.log("You win the game!")
            
        }

        else if (computerScore < playerScore) {
            console.log("Score: " + playerScore);
            console.log("PC Score: " + computerScore);
            console.log("Game over!")
        }

        else if (computerScore == playerScore) {
            console.log("Score: " + playerScore);
            console.log("PC Score: " + computerScore);
            console.log("It's a tie!");
        }

    }
    game();

Hello!

I’ve managed to make it work for five rounds, but without a loop, I don’t know of any way to make it increase the number of rounds if the ‘round’ variable has a value less than 5 because ‘round’ functions as an iterator here and iterators are best used in loops.

I created a helper function named helpGameFunc() that contains the content of your initial iff loop. I also changed a line in there to make sure that the computerSelection value was equal to the returned value from the computerPlay() function you have defined. After that, I called the helperGameFunc() five times within the game() function to carry out 5 whole rounds of the game and get the output, as mentioned on the project page. I hope this helps!

let choices = ["rock", "paper", "scissors"]

//Lets the computer make a choice
function computerPlay() {
    let computerSelection = Math.floor(Math.random() * choices.length);
    return choices[computerSelection].toLowerCase();
}

//The below line was commented out because it is unnecessary to the functioning of the code
//let computerSelection = computerPlay();
let round = 1;
let playerScore = 0;
let computerScore = 0;

//Play a single round
function playRound(playerSelection, computerSelection) {
        
    if (playerSelection == computerSelection) {
        round++;
        return "Draw! Try again!";
    }

    else if (playerSelection == "rock" && computerSelection == "scissors") {
        playerScore++;
        round++;
        return "You win! Nice one!";
    }

    else if (playerSelection == "rock" && computerSelection == "paper") {
        computerScore++;
        round++;
        return "You lose! Ouch!";            
    }

    else if (playerSelection == "paper" && computerSelection == "rock") {
        playerScore++;
        round++;
        return "You win! Nice one!";            
    }

    else if (playerSelection == "paper" && computerSelection == "scissors") {
        computerScore++;
        round++;
        return "You lose! Ouch!";
    }


    else if (playerSelection == "scissors" && computerSelection == "rock") {
        computerScore++;
        round++;
        return "You lose! Ouch!";
    }

    else if (playerSelection == "scissors" && computerSelection == "paper") {
        playerScore++;
        round++;
        return "You win! Nice one!";
    }

    else {
        round--;
        return "What was that? Try again...";
    }
}

//Play a 5-round game


//Helper function which contains the output syntax of a single round
function helpGameFunc() {
    let playerSelection = prompt("Rock, paper, or scissors?").toLowerCase();
    
    //sets the returned value of the computerPlay function to a variable used in the local scope of this function
    let computerSelection = computerPlay();

    console.log("You have chosen " + playerSelection);
    console.log("PC has chosen " + computerSelection);
    console.log("Round: " + round);
    console.log(playRound(playerSelection, computerSelection));
    console.log("Score: " + playerScore);
    console.log("PC Score: " + computerScore);
}

function game() {            

    helpGameFunc();
    helpGameFunc();
    helpGameFunc();
    helpGameFunc();
    helpGameFunc();

    if (playerScore > computerScore) {
        console.log("Score: " + playerScore);
        console.log("PC Score: " + computerScore);
        console.log("You win the game!")
    }

    else if (computerScore < playerScore) {
        console.log("Score: " + playerScore);
        console.log("PC Score: " + computerScore);
        console.log("Game over!")
    }

    else if (computerScore == playerScore) {
        console.log("Score: " + playerScore);
        console.log("PC Score: " + computerScore);
        console.log("It's a tie!");
    }

}
game();
2 Likes

That’s great, thank you so much @error34043!