Console.log not showing expected output for Rock, Paper, Scissors exercise from Odin Project

Viewed 26

I'm creating a rock-paper-scissors game as part of my coursework for Odin Project (https://www.theodinproject.com/lessons/foundations-rock-paper-scissors). I think I'm mostly there but the final step of the project is to count the score and display the winner.

I'm going to attach all the code below with an explanation of where I think the issues are:

    let choices = ["Rock", "Paper", "Scissors"]
let computerSelection = getComputerChoice();

let rockRegex = /rock/i;
let paperRegex = /paper/i;
let scissorsRegex = /scissors/i;
let result = "";

    function getPlayerChoice() {
        prompt("Rock, Paper, Scissors? ")
    }

     function getComputerChoice() {
        let computerChoice = choices[Math.floor(Math.random()*choices.length)];
        // console.log(computerChoice);
        return computerChoice;
     }

     let playerScore = 0;
     let computerScore = 0;
     function playRound(playerSelection, computerSelection) {
        if (rockRegex.test(playerSelection)) {
            if (computerSelection == "Rock") {
                console.log(computerScore);
                return result = "It's a draw! You both had Rock";
            } else if (computerSelection == "Paper") {
                // add score to computer since it won
                computerScore++;
                console.log(computerScore);
                return result = "You lose! Paper beats Rock";
            } else if (computerSelection == "Scissors") {
                // add score to player since you won
                playerScore++;
                console.log(computerScore);
                return result = "You win! Rock beats Scissors";
            }

        } else if (paperRegex.test(playerSelection)) {
            if (computerSelection == "Rock") {
                // add score to player since you won
                playerScore++;
                console.log(computerScore);
                return result = "You win! Paper beats Rock";
            } else if (computerSelection == "Paper") {
                console.log(computerScore);
                return result = "It's a draw! You both had Paper";
            } else if (computerSelection == "Scissors") {
                // add score to computer since you won
                computerScore++;
                console.log(computerScore);
                return result = "You lose! Scissors beat Paper";
            }

        } else if (scissorsRegex.test(playerSelection)) {
            if (computerSelection == "Rock") {
                // add score to computer since you won
                computerScore++;
                console.log(computerScore);
                return result = "You lose! Rock beats Scissors";
            } else if (computerSelection == "Paper") {
                // add score to player since you won
                playerScore++;
                console.log(computerScore);
                return result = "You win! Scissors beats Paper";
            } else if (computerSelection == "Scissors") {
                return result = "It's a draw! You both had Scissors";
            }
        }
     }

     function game() {
        for (let i = 0; i < 2; i++) {
            console.log(result);
            getPlayerChoice();
            playRound();
            console.log("Computer Score:", computerScore, "Your Score:", playerScore);
        }
     }

     game();

I think the issues are in the two functions, playRound and Game. Specifically, I think the issue is in how the values are returned. I think that the playerScore and computerScore variables are not being incremented in the way that they should, perhaps because of some issue with scope. The score should be going up for either player or computer, based on who wins the game of rock paper scissors.

Let me know if there's anything else I can clarify. Thank you everyone :)

1 Answers

ok I see the small issue, you're not storing the prompt value and are not passing it and computer choice to your playRound function.

first

function getPlayerChoice() {
    // add a return here
    return prompt("Rock, Paper, Scissors? ")
}

then

function game() {
        for (let i = 0; i < 2; i++) {
            console.log(result);
            // pass the result of respective functions as agruments to playRound
            playRound(getPlayerChoice(), getComputerChoice());
            console.log("Computer Score:", computerScore, "Your Score:", playerScore);
        }
     }

and thank you for pointing out that quick regex can be stored as a value.. somehow, with how often I use it, i've not been able to do that

Related