Rock,Paper,Scissors Round Tracker

Having a little bit of trouble getting the code on this to loop and start a new round. Not sure what I am missing, but any guidance is appreciated.

// Choices //
let choice = ["rock", "paper", "scissors"];
// Randomly Generate a computer choice //
function getComputerChoice(choice){

    return choice[Math.floor(Math.random() * choice.length)];

}

var computerScore = 0;
var playerScore = 0;
// Playing the round ; determining winners //

function round (playerSelection,computerSelection){
    console.log(playerSelection, computerSelection);



if (playerSelection === computerSelection){
return `It's a tie! You both picked ${playerSelection}!` ;
}
else if (playerSelection === "rock" && computerSelection=== "scissors"){
   playerScore+=1;
    return `You win! ${playerSelection} beats ${computerSelection}`;
}
else if (playerSelection === "paper" && computerSelection=== "rock"){
    playerScore +=1;
    return `You win! ${playerSelection} beats ${computerSelection}`;
}

else if (playerSelection === "scissors" && computerSelection=== "paper"){
   playerScore +=1;
    return `You win! ${playerSelection} beats ${computerSelection}`;
}

else {
    computerScore += 1;
    return `You Lose! ${computerSelection} beats ${playerSelection}`;
}

}



const playerSelection = prompt("Rock, Paper, Or Scissors?").toLowerCase();
const computerSelection = getComputerChoice(choice);

/// shows comparison of what was passed ///
console.log(round(playerSelection,computerSelection));


function game(round) {
    while (playerScore < 5 && computerScore < 5) {
        const playerSelection = prompt("Please enter value").toLowerCase();
        const computerSelection = getComputerChoice(choice);
    }
}

Here is my updated code. I’ve found out how to have the prompt come back up in between rounds, however whenever I input the second choice (i.e “rock” in round 2) im met with an error. The error will be pasted at the bottom of this reply, and is referencing line 6

// Choices //
let choice = ["rock", "paper", "scissors"];
// Randomly Generate a computer choice //
function getComputerChoice(choice){

    return choice[Math.floor(Math.random() * choice.length)];

}
// Score Variable(s)
let computerScore = 0;
let playerScore = 0;
// Playing the round ; determining winners //

function round (playerSelection,computerSelection){
    console.log(playerSelection, computerSelection);



if (playerSelection === computerSelection){
return `It's a tie! You both picked ${playerSelection}!` ;
}
else if (playerSelection === "rock" && computerSelection=== "scissors"){
   playerScore++;
    return `You win! ${playerSelection} beats ${computerSelection}`;
}
else if (playerSelection === "paper" && computerSelection=== "rock"){
    playerScore ++;
    return `You win! ${playerSelection} beats ${computerSelection}`;
}

else if (playerSelection === "scissors" && computerSelection=== "paper"){
   playerScore ++;
    return `You win! ${playerSelection} beats ${computerSelection}`;
}

else {
    computerScore += 1;
    return `You Lose! ${computerSelection} beats ${playerSelection}`;
}

}



const playerSelection = prompt("Rock, Paper, Or Scissors?").toLowerCase();
const computerSelection = getComputerChoice(choice);

/// shows comparison of what was passed ///
console.log(round(playerSelection,computerSelection));


for (let i = 0; i < 5; i++) {
    const playerSelection = prompt("Rock, Paper, Or Scissors?").toLowerCase();
    const computerSelection = getComputerChoice();
    //call round function
    console.log(round(playerSelection, computerSelection));

}

round()


TypeError: undefined is not an object (evaluating 'choice.length')

I found my issue about 30 seconds after my initial reply to myself lol. Heres my updated code. The issue im seeing now is the following;

(i believe) Not keeping track of the userScore and computerScore variables; I attempted to have these show in the console, but it isn’t displaying - leading me to believe that they are not being tracked.

*** update; the console.log is showing up in the console now, however it is evaluating the playerScore and computerScore variables as “undefined” and not updating in between rounds with the ++ ***

// Choices //
console.log(computerScore, playerScore)

console.log("Hello World")

let choice = ["rock", "paper", "scissors"];
// Randomly Generate a computer choice //
function getComputerChoice(choice){

    return choice[Math.floor(Math.random() * choice.length)];

}
// Score Variable(s)
var computerScore = 0;
var playerScore = 0;
// Playing the round ; determining winners //

function round (playerSelection,computerSelection){
    console.log(playerSelection, computerSelection);



if (playerSelection === computerSelection){
return `It's a tie! You both picked ${playerSelection}!` ;
}
else if (playerSelection === "rock" && computerSelection=== "scissors"){
   playerScore++;
    return `You win! ${playerSelection} beats ${computerSelection}`;
}
else if (playerSelection === "paper" && computerSelection=== "rock"){
    playerScore ++;
    return `You win! ${playerSelection} beats ${computerSelection}`;
}

else if (playerSelection === "scissors" && computerSelection=== "paper"){
   playerScore ++;
    return `You win! ${playerSelection} beats ${computerSelection}`;
}

else {
    computerScore += 1;
    return `You Lose! ${computerSelection} beats ${playerSelection}`;
}

}



const playerSelection = prompt("Rock, Paper, Or Scissors?").toLowerCase();
const computerSelection = getComputerChoice(choice);

/// shows comparison of what was passed ///
console.log(round(playerSelection,computerSelection));

while (computerScore<5 && playerScore<5){
for (let i = 0; i < 10; i++) {
    const playerSelection = prompt("Rock, Paper, Or Scissors?").toLowerCase();
    const computerSelection = getComputerChoice(choice);
    //call round function
    console.log(round(playerSelection, computerSelection));
    

}
}
round()

console.log(computerScore, playerScore)

console.log("Hello World")

Final update (for now)

// Choices //
console.log(computerScore, playerScore)

console.log("Hello World")

let choice = ["rock", "paper", "scissors"];
// Randomly Generate a computer choice //
function getComputerChoice(choice){

    return choice[Math.floor(Math.random() * choice.length)];

}
// Score Variable(s)
var computerScore = 0;
var playerScore = 0;
// Playing the round ; determining winners //

function round (playerSelection,computerSelection){
    console.log(playerSelection, computerSelection);



if (playerSelection === computerSelection){
return `It's a tie! You both picked ${playerSelection}!` ;
}
else if (playerSelection === "rock" && computerSelection=== "scissors"){
   playerScore++;
    return `You win! ${playerSelection} beats ${computerSelection}`;
}
else if (playerSelection === "paper" && computerSelection=== "rock"){
    playerScore ++;
    return `You win! ${playerSelection} beats ${computerSelection}`;
}

else if (playerSelection === "scissors" && computerSelection=== "paper"){
   playerScore ++;
    return `You win! ${playerSelection} beats ${computerSelection}`;
}

else {
    computerScore += 1;
    return `You Lose! ${computerSelection} beats ${playerSelection}`;
}

}



const playerSelection = prompt("Rock, Paper, Or Scissors?").toLowerCase();
const computerSelection = getComputerChoice(choice);

/// shows comparison of what was passed ///
console.log(round(playerSelection,computerSelection));

while (computerScore<4 && playerScore<4){
for (let i = 0; i < 10; i++) {
    const playerSelection = prompt("Rock, Paper, Or Scissors?").toLowerCase();
    const computerSelection = getComputerChoice(choice);
    //call round function
    console.log(round(playerSelection, computerSelection));
    console.log(playerScore, computerScore)


}
}
round()

console.log(computerScore, playerScore)

console.log("Hello World")

I believe I have taken great steps today. Score is being returned after every round. Game terminates after round 10. Any revisions you would make to the code for quality of life enhancements prior to HTML & CSS styling?

The for loop appears extraneous, it will cause the game to go a minimum of 10 rounds regardless if there is a winner after as few as 4 rounds.
What is the purpose of the call to round() after the while loop?
The code to get the player and computer plays before the while loop and the call to round are also redundant and not needed.

You might consider validating the input from the player if they don’t enter “rock”, “paper” or “scissors”, although they lose the round if they don’t.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.