Currently stuck with a rock, paper, scissors game in javascript. Only my last function game() which needs to play a round 5 times is not working. Have tried to work with a for loop but it seems that I'm getting 5 times the same answer instead of 5 different random ones.
Could someone please help me out?
let playerScore = 0; let computerScore = 0; const playerSelection = playerPlay(); const computerSelection = computerPlay(); // computer select function function computerPlay() { const option = ['rock', 'paper', 'scissors']; let random = Math.floor(Math.random() * 3); return option[random]; } console.log('Computer: ' + computerSelection); // player select function function playerPlay() { const input = prompt('Please enter input'); const option = input.toLowerCase(); return option; } console.log('Player: ' + playerSelection); // play 1 single round function playRound(playerSelection, computerSelection) { if(playerSelection === computerSelection) { return 'It is a tie'; } if(playerSelection === 'rock') { if(computerSelection === 'scissors') { playerScore++; return 'Player wins with rock'; } else if(computerSelection === 'paper') { computerScore++; return 'Computer wins with paper' } } if(playerSelection === 'paper') { if(computerSelection === 'rock') { playerScore++; return 'Player wins with paper'; } else if(computerSelection === 'scissors') { computerScore++; return 'Computer wins with scissors'; } } if(playerSelection === 'scissors') { if(computerSelection === "paper") { playerScore++; return 'Player wins with scissors'; } else if(computerSelection === 'rock') { computerScore++; return 'Computer wins with rock'; } } } // console.log(playRound(playerSelection, computerSelection)); playRound(playerSelection, computerSelection); console.log('Player: ' + playerScore); console.log('Computer: ' + computerScore); // game function game() { for(var i = 1; i <= 5; i++) { console.log('repeat 5 times'); playRound(playerSelection, computerSelection); } } game();
You have put a loop without asking for the player and computer input, hence it runs 5 times without taking any input. I have fixed this in the snippet. Not entirely sure if you want to run it this way though.
let playerScore = 0; let computerScore = 0; // computer select function function computerPlay() { const option = ['rock', 'paper', 'scissors']; let random = Math.floor(Math.random() * 3); return option[random]; } // player select function function playerPlay() { const input = prompt('Please enter input'); const option = input.toLowerCase(); return option; } // play 1 single round function playRound(playerSelection, computerSelection) { if (playerSelection === computerSelection) { return 'It is a tie'; } if (playerSelection === 'rock') { if (computerSelection === 'scissors') { playerScore++; return 'Player wins with rock'; } else if (computerSelection === 'paper') { computerScore++; return 'Computer wins with paper' } } if (playerSelection === 'paper') { if (computerSelection === 'rock') { playerScore++; return 'Player wins with paper'; } else if (computerSelection === 'scissors') { computerScore++; return 'Computer wins with scissors'; } } if (playerSelection === 'scissors') { if (computerSelection === "paper") { playerScore++; return 'Player wins with scissors'; } else if (computerSelection === 'rock') { computerScore++; return 'Computer wins with rock'; } } } // console.log(playRound(playerSelection, computerSelection)); // game function game() { for (i = 0; i <= 5; i++) { var playerSelection = playerPlay(); var computerSelection = computerPlay(); playRound(playerSelection, computerSelection); console.log('Computer: ' + computerSelection); console.log('Player: ' + playerSelection); console.log('Player: ' + playerScore); console.log('Computer: ' + computerScore); } console.log('Final Player: ' + playerScore); console.log('Final Computer: ' + computerScore); } game();
Your probleme is because you're set your playerSelection
and computerSelection
once (and even on a const ! So choice cannot be updated)
You just have to move this part into you for
loop (and update to let
instead of const
)
let playerSelection = playerPlay();
let computerSelection = computerPlay();
let playerScore = 0; let computerScore = 0; // computer select function function computerPlay() { const option = ['rock', 'paper', 'scissors']; let random = Math.floor(Math.random() * 3); return option[random]; } // player select function function playerPlay() { const input = prompt('Please enter input'); const option = input.toLowerCase(); return option; } // play 1 single round function playRound(playerSelection, computerSelection) { if(playerSelection === computerSelection) { return 'It is a tie'; } if(playerSelection === 'rock') { if(computerSelection === 'scissors') { playerScore++; return 'Player wins with rock'; } else if(computerSelection === 'paper') { computerScore++; return 'Computer wins with paper' } } if(playerSelection === 'paper') { if(computerSelection === 'rock') { playerScore++; return 'Player wins with paper'; } else if(computerSelection === 'scissors') { computerScore++; return 'Computer wins with scissors'; } } if(playerSelection === 'scissors') { if(computerSelection === "paper") { playerScore++; return 'Player wins with scissors'; } else if(computerSelection === 'rock') { computerScore++; return 'Computer wins with rock'; } } } // game function game() { for(var i = 1; i <= 5; i++) { let playerSelection = playerPlay(); let computerSelection = computerPlay(); console.log(`[Play turn ${i}] Player: ${playerSelection} | Computer: ${computerSelection}`); playRound(i); } } game();
Your code has a few issues.
You don't display output of next games. They are played but result is the same. It's same game configuration repeated 5 times.
console.log('repeat 5 times'); console.log(playRound(playerSelection, computerSelection));
Instead of:
console.log('repeat 5 times');
console.log(playRound(playerSelection, computerSelection));
playerSelection
, computerSelection
only once. So every next play has the same result.You should execute these functions with every loop iteration.
Example:
let playerSelection = function () {
const input = prompt('Please enter input');
const option = input.toLowerCase();
return option;
};
let computerSelection = function () {
const option = ['rock', 'paper', 'scissors'];
let random = Math.floor(Math.random() * 3);
return option[random];
};
[...]
playRound(playerSelection(), computerSelection());
Complete Rock, Paper, Scissors game in React js https://reseachershan.github.io/rock-paper-sissor/ to get the code check out my repo https://github.com/Reseachershan/rock-paper-sissor
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.