I made corrections to the script and pasted it here. You can read the comments attached to each correction.
function computerPlay() {
const select = ["rock", "paper", "scissors"]
return select[Math.floor(Math.random() * select.length)];
}
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection) {
return " it is a tie";
}
if (playerSelection === 'rock' && computerSelection === 'scissors') {
return "you win rock beat scissors"
}
if (playerSelection === 'paper' && computerSelection === 'rock') {
return "you win paper beat rock"
}
if (playerSelection === 'scissors' && computerSelection === 'paper') {
return "you win scissor beat paper"
}
if (computerSelection === 'rock' && playerSelection === 'scissors') {
return "you loss rock beat scissors"
}
if (computerSelection === 'paper' && playerSelection === 'rock') {
return "you loss paper beat rock"
}
if (computerSelection === 'scissors' && playerSelection === 'paper') {
return "you loss scissors beat paper "
}
}
// let playerSelection;
// You can remove this playerSelection definition here
// and instead define it in the loop as it is only needed in the loop
function game() {
let computerScore = 0;
let playerScore = 0;
let score = computerScore + ' ' + playerScore;
for (i = 0; i <= 4; i++) {
const computerSelection = computerPlay()
// Put the variable for computerSelection within the loop so everytime the loop runs its a different computer selection.
const playerSelection = prompt('enter a selection');
let result = playRound(playerSelection, computerSelection)
// Since playRound is being called multiple times you can set it as a variable and use it wherever you need it
console.log(result);
// First remove this console line as playRound function is already being running in the if conditional.
if (result.includes('you win')) {
score = computerScore + ' ' + playerScore++;
console.log(score);
} else if (result.includes('you loss')) {
score = computerScore++ + ' ' + playerScore;
console.log(score)
} else if (result.includes('tie')) {
console.log(score)
}
}
if (computerScore > playerScore) {
console.log('You loss')
} else if (playerScore > computerScore) {
console.log('You win');
} else if (playerScore == computerScore) {
console.log('it a draw')
}
}
game();
I made corrections to the script and pasted it here. You can read the comments attached to each correction.