hope you all doing well. I am making a "Rock Paper Scissors" game for 'TheOdinProject', I completed it but here is the problem with loop, when I use it for console it works fine, and when calling a function its not looping. sorry for the bad english.
The last function game() is for looping, please help me to find the problem. thanks in advance.
function getComputerChoice() {
const gameChoice = ['rock', 'paper', 'scissor'];
const randomChoice = gameChoice[Math.floor(Math.random() * gameChoice.length)];
// console.log(randomChoice);
return randomChoice;
}
function playRound(playerSelection, computerSelection) {
switch (true) {
case (playerSelection === computerSelection):
return console.log('Game Tie, please try again.');
break;
case (playerSelection === 'rock' && computerSelection === 'scissor'):
let greetingRock = `You Choose: ${playerSelection} and`;
return console.log(`${greetingRock} You Win, Rock beats the scissor`);
break;
case (playerSelection === 'rock' && computerSelection === 'paper'):
let courageRock = `You Choose: ${playerSelection} and`;
return console.log(`${courageRock} You Lose, Paper beats the Rock`);
break;
case (playerSelection === 'scissor' && computerSelection === 'rock'):
let courageScissor = `You Choose: ${playerSelection} and`;
return console.log(`${courageScissor} You Lose, Rock beats the scissor`);
break;
case (playerSelection === 'scissor' && computerSelection === 'paper'):
let greetingScissor = `You Choose: ${playerSelection} and`;
return console.log(`${greetingScissor} You Win, Scissor beats the Paper`);
break;
case (playerSelection === 'paper' && computerSelection === 'scissors'):
let couragePaper = `You Choose: ${playerSelection} and`;
return console.log(`${couragePaper} You Lose, Scissor beats the Paper`);
break;
case (playerSelection === 'paper' && computerSelection === 'rock'):
let greetingPaper = `You Choose: ${playerSelection} and`;
return console.log(`${greetingPaper} You Win, Paper beats the Rock`);
break;
}
}
let userValue = prompt('Choose your Item:', '').toLowerCase();
if( userValue == null || userValue == "") {
prompt('Please Choose one of: Rock, Paper, scissor')
}
const playerSelection = userValue;
const computerSelection = getComputerChoice();
// console.log(playRound(playerSelection, computerSelection))
function game() {
for ( let i = 0; i < 5; i++ ) {
let playAgain = playRound(playerSelection, computerSelection);
return playAgain;
}
}
game();
// console.log(game());
function getComputerChoice() {
const gameChoice = ['rock', 'paper', 'scissor'];
const randomChoice = gameChoice[Math.floor(Math.random() * gameChoice.length)];
// console.log(randomChoice);
return randomChoice;
}
function playRound(playerSelection, computerSelection) {
switch (true) {
case (playerSelection === computerSelection):
console.log('Game Tie, please try again.');
break;
case (playerSelection === 'rock' && computerSelection === 'scissor'):
let greetingRock = `You Choose: ${playerSelection} and`;
console.log(`${greetingRock} You Win, Rock beats the scissor`);
break;
case (playerSelection === 'rock' && computerSelection === 'paper'):
let courageRock = `You Choose: ${playerSelection} and`;
console.log(`${courageRock} You Lose, Paper beats the Rock`);
break;
case (playerSelection === 'scissor' && computerSelection === 'rock'):
let courageScissor = `You Choose: ${playerSelection} and`;
console.log(`${courageScissor} You Lose, Rock beats the scissor`);
break;
case (playerSelection === 'scissor' && computerSelection === 'paper'):
let greetingScissor = `You Choose: ${playerSelection} and`;
console.log(`${greetingScissor} You Win, Scissor beats the Paper`);
break;
case (playerSelection === 'paper' && computerSelection === 'scissors'):
let couragePaper = `You Choose: ${playerSelection} and`;
console.log(`${couragePaper} You Lose, Scissor beats the Paper`);
break;
case (playerSelection === 'paper' && computerSelection === 'rock'):
let greetingPaper = `You Choose: ${playerSelection} and`;
console.log(`${greetingPaper} You Win, Paper beats the Rock`);
break;
}
}
function getPlayerChoice() {
const userValue = prompt('Choose your Item:', '').toLowerCase();
if (userValue == null || userValue == '') {
prompt('Please choose one of: Rock, Paper, Scissor');
}
const playerSelection = userValue;
const computerSelection = getComputerChoice();
console.log(playRound(playerSelection, computerSelection));
}
function game() {
let playAgain;
for (let i = 0; i < 5; i++) {
playAgain = getPlayerChoice();
}
}
game()
Related
I am writing this simple console game Rock-Paper-Scissors for TheOdinProject course
I'm having a problem with with the computerSelection not being updated in loop.
There should be 5 rounds in which a player enters rock/paper/scissors in the prompt window and it gets compared against randomized computer answer.
The overall logic of the game seems to be working fine but the problem is that computerSelection stays the same every turn once generated in round 1 but the prompt updates playerSelection as it should.
Here's the code:
const rock = 'rock';
const paper = 'paper';
const scissors = 'scissors';
let playerSelection = '';
const computerSelection = getComputerChoice();
function getComputerChoice() {
let items = [rock, paper, scissors];
let random = Math.floor(Math.random() * 3);
let choice = items[random];
return choice;
}
function playRound(playerSelection, computerSelection) {
if (playerSelection == computerSelection) {
roundResult = "It's a Draw";
} else if (playerSelection == rock && computerSelection == paper) {
roundResult = "Computer wins this round! Paper beats rock!";
} else if (playerSelection == rock && computerSelection == scissors) {
roundResult = "Human wins this round! Rock beats scissors!";
} else if (playerSelection == paper && computerSelection == rock) {
roundResult = "Human wins this round! Paper beats rock!"
} else if (playerSelection == paper && computerSelection == scissors) {
roundResult = "Computer wins this round! Scissors beats paper";
} else if (playerSelection == scissors && computerSelection == rock) {
roundResult = "Computer wins this round! Rock beats scissors!";
} else if (playerSelection == scissors && computerSelection == paper) {
roundResult = "Human wins this round! Scissors beats paper!";
}
return roundResult;
}
function game() {
for (let i = 0; i < 5; i++) {
playRound(prompt("What do you choose?"), computerSelection);
console.log(roundResult);
}
}
onsole.log("Machine: " + computerSelection);
console.log(game());
as #Barmar suggested, all I had to do was put the
const computerSelection = getComputerChoice();
inside the loop.
const rock = 'rock';
const paper = 'paper';
const scissors = 'scissors';
let playerSelection = '';
const computerSelection = getComputerChoice();
function getComputerChoice() {
let items = [rock, paper, scissors];
let random = Math.floor(Math.random() * 3);
let choice = items[random];
return choice;
}
function playRound(playerSelection, computerSelection) {
if (playerSelection == computerSelection) {
roundResult = "It's a Draw";
} else if (playerSelection == rock && computerSelection == paper) {
roundResult = "Computer wins this round! Paper beats rock!";
} else if (playerSelection == rock && computerSelection == scissors) {
roundResult = "Human wins this round! Rock beats scissors!";
} else if (playerSelection == paper && computerSelection == rock) {
roundResult = "Human wins this round! Paper beats rock!"
} else if (playerSelection == paper && computerSelection == scissors) {
roundResult = "Computer wins this round! Scissors beats paper";
} else if (playerSelection == scissors && computerSelection == rock) {
roundResult = "Computer wins this round! Rock beats scissors!";
} else if (playerSelection == scissors && computerSelection == paper) {
roundResult = "Human wins this round! Scissors beats paper!";
}
return roundResult;
}
function game() {
for (let i = 0; i < 5; i++) {
const computerSelection = getComputerChoice();
playRound(prompt("What do you choose?"), computerSelection);
console.log(roundResult);
}
}
console.log("Machine: " + computerSelection);
game();
Pass the getComputerChoice() in the playground args.
playRound(prompt("What do you choose?"), getComputerChoice());
I'm working on Rock Paper Scissors and I'm lost at how to keep and display scores as well as determine a winner when they reach a score of 5. Right now, I'm adding +1 to the playerScore or compScore depending on the single round result, but I don't know how to display or keep track of it. Here is my code so far. Any tips are greatly appreciated!
//choices array
const choices = ["rock", "paper", "scissors"];
//set scores to 0
let playerScore = 0;
let compScore = 0;
//Get random choice from comp
function getComputerChoice() {
const randomNum = Math.floor(Math.random() * 3) //Generate random num with Math.random(), to ensure it's between 0 and 3, multiply by 3. Use Math.floor to round to down to nearest num
switch (randomNum) { //Create switch statement to take randomNum variable to perform different action based on the condition (case)
case 0:
return "rock";
case 1:
return "paper";
case 2:
return "scissors";
}
}
//single round gameplay
function playRound(playerSelection, computerSelection) {
if (playerSelection == "rock" && computerSelection == "scissors") {
//Add 1 to playerScore
playerScore+= 1
return "You win! Rock beats scissors";
} else if (playerSelection == "rock" && computerSelection == "paper") {
//Add 1 to compScore
compScore+= 1
return "You lose. Paper beats rock";
} else if (playerSelection == "scissors" && computerSelection == "paper") {
playerScore+= 1
return "You win! Scissors beat paper";
} else if (playerSelection == "scissors" && computerSelection == "rock") {
compScore+= 1
return "You lose. Rock beats scissors";
} else if (playerSelection == "paper" && computerSelection == "rock") {
playerScore+= 1
return "You win! Paper beats rock";
} else if (playerSelection == "paper" && computerSelection == "scissors") {
compScore+= 1
return "You lose. Scissors beat paper";
} else if (playerSelection === computerSelection) {
return "It's a tie";
} else {
return "Try again";
}
}
function userInput() {
let ask = false;
//while ask is still false, continue looping
while (ask == false){
const selction = prompt("Rock Paper or Scissors?")
// if the prompt return is empty
if (selction == null){
//keep looping
continue;
}
const selctionLower = selction.toLowerCase();
//ask if array of choices [rock, paper, scissors] is in the user input
if (choices.includes(selctionLower)){
//then ask is true
ask = true;
return selctionLower;
}
}
}
function game() {
//loop
for (let i = 0; i < 5; i++) {
const playerSelection = userInput();
const computerSelection = getComputerChoice();
//call playRound function
console.log(playRound(playerSelection, computerSelection));
}
}
game()
I'm trying to make a console version of rock paper scissors. The game is working for now but I can't run the for loop, what I want to do is at the end of each round, the console will say what round we are in and tell who won the current round. It will be the Best of 5 and the winner will be determined in round 5.
Since I'm a beginner I couldn't try too much. I know my code is not clean and complicated because I proceed by trying and I'm also new to Stackoverflow, if there is something missing in the topic, I apologize in advance for those.
// PLAYER CODES
let playerSelection = prompt('Do you choose Rock, Paper or Scissors?')
if (playerSelection === 'Rock' || playerSelection === 'Paper' || playerSelection === 'Scissors') {
console.log("You chose " + `${playerSelection}`);
} else {
console.log('Error! Pick Rock, Paper or Scissors')
}
// COMPUTER CODES
let computerSelection = getComputerChoice();
function getComputerChoice() {
let choices = ['Rock', 'Paper', 'Scissors'];
let random = choices[Math.floor(Math.random() * 3)];
return random;
}
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection){
return ('Draw')
} else if ((playerSelection === 'Rock') && (computerSelection === 'Scissors')) {
return ('Player Wins! Rock beats Scissors.')
} else if ((playerSelection === 'Scissors') && (computerSelection === 'Paper')) {
return ('Player Wins! Scissors beats Paper.')
} else if ((playerSelection === 'Paper') && (computerSelection === 'Rock')) {
return ('Player Wins! Paper beats Rock.')
} else if ((computerSelection === 'Scissors') && (playerSelection === 'Paper')) {
return ('Computer Wins! Scissors beats Paper.')
} else if ((computerSelection === 'Paper') && (playerSelection === 'Rock')) {
return ('Computer Wins! Paper beats Rock.')
} else if ((computerSelection === 'Rock') && (playerSelection === 'Scissors')) {
return ('Computer Wins! Rock beats Scissors.')
}
}
function game() {
for (let i = 1; i < 6; i++){
playRound(playerSelection, computerSelection);
if (i == 1)
return ('Round 1')
else if (i == 2)
return ('Round 2')
else if (i == 3)
return ('Round 3')
else if (i == 4)
return ('Round 4')
else if (i == 5)
return ('Round 5')
}
}
console.log("Computer chose " + `${computerSelection}`);
console.log(playRound(playerSelection, computerSelection));
console.log(game(playRound));
You have to put the sections to get the player and computer choices inside the loop. I fixed some other problems, too.
// PLAYER CODES
function getPlayerChoice () {
let playerSelection = prompt('Do you choose Rock, Paper or Scissors?')
if (playerSelection === 'Rock' || playerSelection === 'Paper' || playerSelection === 'Scissors') {
console.log("You chose " + `${playerSelection}`);
return playerSelection
} else {
console.log('Error! Pick Rock, Paper or Scissors')
return getPlayerChoice()
}
}
// COMPUTER CODES
function getComputerChoice() {
let choices = ['Rock', 'Paper', 'Scissors'];
let random = choices[Math.floor(Math.random() * 3)];
return random;
}
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection){
return ('Draw')
} else if ((playerSelection === 'Rock') && (computerSelection === 'Scissors')) {
return ('Player Wins! Rock beats Scissors.')
} else if ((playerSelection === 'Scissors') && (computerSelection === 'Paper')) {
return ('Player Wins! Scissors beats Paper.')
} else if ((playerSelection === 'Paper') && (computerSelection === 'Rock')) {
return ('Player Wins! Paper beats Rock.')
} else if ((computerSelection === 'Scissors') && (playerSelection === 'Paper')) {
return ('Computer Wins! Scissors beats Paper.')
} else if ((computerSelection === 'Paper') && (playerSelection === 'Rock')) {
return ('Computer Wins! Paper beats Rock.')
} else if ((computerSelection === 'Rock') && (playerSelection === 'Scissors')) {
return ('Computer Wins! Rock beats Scissors.')
}
}
function game() {
for (let i = 1; i < 6; i++){
console.log('Round ' + i)
let playerSelection = getPlayerChoice();
let computerSelection = getComputerChoice();
console.log("Computer chose " + `${computerSelection}`);
console.log(playRound(playerSelection, computerSelection));
}
}
game()
I'm having problems getting my simple Rock Paper Scissors game to display the winner of each game when the user chooses anything either than Rock. How do I use multiple if...else statements so that my declareWinner function works when the user chooses Scissors or Paper?
let choices = ["rock", "paper", "scissors"];
// GETTING USER INPUT
const userInput = prompt("Do you choose rock, paper or scissors?").toLocaleLowerCase();
if(userInput ==="paper" || userInput ==="rock" || userInput ==="scissors"){
console.log("You chose " + `${userInput}`);
} else {
console.log("Error! Try again!");
}
// GETTING COMPUTER INPUT
const computerInput = choices[Math.floor(Math.random() * 3)];
console.log("Computer chose" + ` ${computerInput}`);
if (computerInput <= 0.33) {
computerInput = "rock";
}
if (computerInput >= 0.67) {
computerInput = "paper";
}
if (computerInput >= 0.66) {
computerInput = "scissors";
}
// DECLARE WINNER
// User Chooses Rock
function declareWinner(userInput, computerInput) {
if(userInput === "rock" && computerInput === 'paper') {
console.log ('You win! Rock beats paper!');
} else if (userInput === 'rock' && computerInput === 'rock') {
console.log ('Its a tie!');
} else if (userInput === 'rock' && computerInput === 'scissors') {
console.log ('You win! Rock beats scissors!');
// User Chooses Paper
} else if(userInput === "paper" && computerInput === 'rock') {
console.log ('You win! Rock beats paper!');
} else if (userInput === 'paper' && computerInput === 'paper') {
console.log ('Its a tie!');
} else if (userInput === 'paper' && computerInput === 'scissors') {
console.log ('You lose! Scissors beats paper!');
// User Chooses Scissors
} else if(userInput === "scissors" && computerInput === 'rock') {
console.log ('You lose! Rock beats scissors!');
} else if (userInput === 'scissors' && computerInput === 'paper') {
console.log ('You win! Scissors beats paper!');
} else {
console.log ('It is a tie!');
}
}
declareWinner();
There were two problems with the code:-
You are doing choices[Math.floor(Math.random() * 3)]; which results in a random value from choices array, so there is no need for multiple if-else statements that were after it.
You were missing the declareWinner function declaration.
Also, You are missing some conditions in declareWinner function, like userInput === 'paper' && computerInput === 'rock', userInput === 'rock' && computerInput === 'scissors' etc.
const choices = ["rock", "paper", "scissors"];
// GETTING USER INPUT
const userInput = prompt("Do you choose rock, paper or scissors?");
if (userInput === "paper" || userInput === "rock" || userInput === "scissors") {
console.log("You chose " + `${userInput}`);
} else {
console.log("Error! Try again!");
}
// GETTING COMPUTER INPUT
let computerInput = choices[Math.floor(Math.random()*3)];
console.log("Computer chose" + ` ${computerInput}`);
// DECLARE WINNER
const winner = declareWinner(userInput, computerInput);
function declareWinner(userInput, computerInput) {
if (userInput === 'rock' && computerInput === 'paper') {
console.log('You lose! Rock beats Paper');
} else if (userInput === 'scissors' && computerInput === 'paper') {
console.log('You win! Scissors beats Paper');
} else {
console.log('You tie!');
}
}
Tip:- You can use an object to choose the winner in declareWinner function.
const choices = ["rock", "paper", "scissors"];
// GETTING USER INPUT
const userInput = prompt("Do you choose rock, paper or scissors?");
if (userInput === "paper" || userInput === "rock" || userInput === "scissors") {
console.log("You chose " + `${userInput}`);
} else {
console.log("Error! Try again!");
}
// GETTING COMPUTER INPUT
let computerInput = choices[Math.floor(Math.random() * 3)];
console.log("Computer chose" + ` ${computerInput}`);
// DECLARE WINNER
const winner = declareWinner(userInput, computerInput);
function declareWinner(userInput, computerInput) {
let win = {
rock: "scissors",
paper: "rock",
scissors: "paper"
};
if (win[userInput] === computerInput) {
console.log(`You won! ${userInput} beats ${computerInput}`);
} else if (win[computerInput] === userInput) {
console.log(`You lost! ${computerInput} beats ${userInput}`);
} else console.log("Tie")
}
You can't change a const value. Use let instead:
let computerInput = choices[Math.floor(Math.random() * 3)];
I am creating a rock paper scissors game where I have a playRound() function. I am trying to create a game() function where I can call this playRound() function multiple times with different results.
I want to use this game() function to play multiple rounds in order to find a winner
but I am still stuck on getting the playRound() to run multiple times.
With my current code, the playRound() function only runs once in the console.
Here is my code :
function playRound(playerSelection, computerSelection) {
const lose = "You Lose! Paper beats rock";
const win = "You Win! rock beats paper";
const tie = "its a tie";
console.log(`Your selection is : ${playerSelection}`);
console.log(`PC selection is : ${computerSelection}`);
if (playerSelection === playerSelection && computerSelection === "paper") {
return lose;
} else if (
playerSelection === playerSelection &&
computerSelection === "scissors"
) {
return win;
} else {
return tie;
}
}
function game() {
for (let i = 0; i < 3; i++) {
return playRound(playerSelection, computerSelection);
}
}
function computerPlay() {
let choices = ["rock", "paper", "scissors"];
let choice = choices[Math.floor(Math.random() * choices.length)];
return choice;
}
const string = "ROck";
const playerSelection = string.toLowerCase() || string.toUpperCase();
const computerSelection = computerPlay();
console.log(game());
Not only is your game() function returning the results of the round thus leaving the loop... but you are setting the computerSelection only once... before calling the game() function.. so neither playerSelection or computerSelection ever changes.
I rearranged your code a bit and added an overall game score. Take a look.
function playRound(playerSelection, round) {
console.log("Round " + round);
const computerSelection = computerPlay();
console.log(`Your selection is : ${playerSelection}`);
console.log(`PC selection is : ${computerSelection}`);
if (playerSelection === playerSelection && computerSelection === "paper") {
return 'lose';
} else if (
playerSelection === playerSelection &&
computerSelection === "scissors"
) {
return 'win';
} else {
return 'tie';
}
}
function game() {
const lose = "You Lose! Paper beats rock";
const win = "You Win! Rock beats scissors";
const tie = "It's a tie";
let playerScore = 0;
let computerScore = 0;
for (let i = 1; i < 4; i++) {
const result = playRound(playerSelection, i);
switch (result) {
case 'win':
console.log(win);
playerScore++;
break;
case 'lose':
console.log(lose);
computerScore++;
break;
default:
console.log(tie);
playerScore++;
computerScore++;
break;
}
}
console.log("Final Results: Player: " + playerScore + " Computer: " + computerScore);
if (playerScore > computerScore) {
console.log("You win the game!");
} else if (playerScore < computerScore) {
console.log("You lose the game.");
} else {
console.log("The game was an overall tie.");
}
}
function computerPlay() {
let choices = ["rock", "paper", "scissors"];
let choice = choices[Math.floor(Math.random() * choices.length)];
return choice;
}
const playerSelection = "rock";
game();