This question is directly related to The Odin Project Rock Paper Scissors JavaScript Fundamentals course.
I have successfully made a function which randomly picks "rock," "paper," or "scissors" for the computer, called "computerPlay."
function computerPlay() {
const gameArray = ['Rock', 'Paper', 'Scissors'];
const random = Math.floor(Math.random() * gameArray.length);
return gameArray[random];
}
I have made another function to accept the player's input of "rock," "paper," "scissors" (or 1, 2, 3 respectively) with no respect for casing in another function, called "playerPlay."
function playerPlay() {
console.log ("Do you choose 'Rock' (1), 'Paper' (2), or 'Scissors' (3)?");
let playerChoice = prompt("Do you choose 'Rock' (1), 'Paper' (2), or 'Scissors' (3)?");
if (playerChoice.toLowerCase() === "rock" || parseInt(playerChoice) === 1) {
playerChoice = "Rock";
} else if (playerChoice.toLowerCase() === "paper" || parseInt(playerChoice) === 2) {
playerChoice = "Paper";
}
else if (playerChoice.toLowerCase() === "scissors" || parseInt(playerChoice) === 3) {
playerChoice = "Scissors";
}
else if (playerChoice.toLowerCase() !== "rock" || "scissors" || "paper" || parseInt(playerChoice) !== 1 || 2 || 3) {
alert("Please try to enter your value again :)")
playerSelection();
}
else {
console.log("Not sure what's going on, hold on to yer butts ;)")
}
return playerChoice;
}
The function playerPlay() is saved into the const variable playerSelection, and the frunction computerPlay() is saved into the const variable computerSelection.
// Create a variable to store player's choice
const playerSelection = playerPlay();
// Create a variable to store computer's choice
const computerSelection = computerPlay();
These two const variables are then used in the playRound() function which runs a set of comparisons to see and then console.log and return whether the player or computer won the round of rock, papers, scissors.
/* Write a function that plays a single round of Rock Paper Scissors. The function should take two parameters - the playerSelection and
computerSelection - and then return a string that declares the winner of the round like so: "You Lose! Paper beats Rock" */
function playRound(playerSelection, computerSelection) {
if (computerSelection.toLowerCase() === playerSelection.toLowerCase()) {
console.log("It's a draw!");
return("Your Choice: " + playerSelection + ". " + "Computer: " + computerSelection);
} else if (computerSelection.toLowerCase() === "rock" && playerSelection.toLowerCase() === "scissors") {
console.log("Rock beats scissors! You lose :(");
return("Your Choice: " + playerSelection + ". " + "Computer: " + computerSelection);
} else if (computerSelection.toLowerCase() === "scissors" && playerSelection.toLowerCase() === "rock") {
console.log("Rock beats scissors! You win :D");
return("Your Choice: " + playerSelection + ". " + "Computer: " + computerSelection);
} else if (computerSelection.toLowerCase() === "scissors" && playerSelection.toLowerCase() === "paper") {
console.log("Scissors beats paper! You lose :(");
return("Your Choice: " + playerSelection + ". " + "Computer: " + computerSelection);
} else if (computerSelection.toLowerCase() === "paper" && playerSelection.toLowerCase() === "scissors") {
console.log("Scissors beats paper! You win :D");
return("Your Choice: " + playerSelection + ". " + "Computer: " + computerSelection);
} else if (computerSelection.toLowerCase() === "paper" && playerSelection.toLowerCase() === "rock") {
console.log("Paper beats rock! You lose :(");
return("Your Choice: " + playerSelection + ". " + "Computer: " + computerSelection);
} else if (computerSelection.toLowerCase() === "rock" && playerSelection.toLowerCase() === "paper") {
console.log("Paper beats rock! You win :D");
return("Your Choice: " + playerSelection + ". " + "Computer: " + computerSelection);
} else {
console.log("I'm not sure what, but something went worng :(");
}
}
My issue comes from when I need to repeat the playRound() function 5 times, and have each round be different.
/* Write a NEW function called game(). Use the previous function inside of this one to play a 5 round game that keeps score and reports
a winner or loser at the end. */
// Define function / rule for winning and keeping track of score and determining winner for 5 rounds of game
// Make each round reset the computer choice and get new input from the player.
function game() {
roundOne;
roundTwo;
roundThree;
roundFour;
roundFive;
}
const roundOne = playRound(playerSelection, computerSelection);
const roundTwo = playRound(playerSelection, computerSelection);
const roundThree = playRound(playerSelection, computerSelection);
const roundFour = playRound(playerSelection, computerSelection);
const roundFive = playRound(playerSelection, computerSelection);
// Print results of game round
console.log(game(playerSelection, computerSelection));
I recently tried to make 5 new const variables (roundOne, roundTwo... roundFive) to contain the "playRound(playerSelection, computerSelection);" function. I tried also changing the playerSelection and computerSelection const variables to let, thinking the const nature could be locking the values in place until I reload the page. I thought it might be a scope issue, but I am not understanding where exactly it is -- which is why I thought to ask here. The main issue is that the player-entered value and the computer randomly-generated value are always the same for the 5 repeated rounds. How do I get a different value to print for each repeated round? Does it maybe have to do with how I use "gameArray" (in computerPlay() function) and "playerChoice" (in playerPlay() function)?
I apologize if this much longer than it needs to be. I will continue looking into things as well. Thank you very much in advance.
(This is my first actual JavaScript creation/project of any kind, so any feedback in general, or in how I posted/asked this question as well, would also be greatly appreciated. Thank you!)