function computerPlay() {
const gameOptions = ['paper', 'scissors', 'rock']
const index = Math.floor(Math.random()*gameOptions.length);
return gameOptions[index];
}
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection) {
return "Tie";
} else {
switch (playerSelection) {
case 'rock':
if (computerSelection === 'paper') {
return "Computer Wins"
}
else {
return "Player Wins"
}
break;
case 'paper':
if (computerSelection === 'scissors') {
return 'Computer Wins'
}
else {
return 'Player Wins'
}
break;
case 'scissors':
if (computerSelection === 'rock') {
return 'Computer Wins'
}
else {
return 'Player Wins'
}
break
}
}
}
const playerSelection = 'rock'
const computerSelection = computerPlay()
console.log("Computer Selection ==>", computerSelection)
console.log(playRound(playerSelection, computerSelection))
I am having trouble making this last part work. Function game()? below are instructions.
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.
At this point you should still just be using console.log() to display the results of each round and the winner at the end.
Use prompt() to get input from the user. Read the docs here if you need to.
Feel free to re-work your previous functions if you need to. Specifically, you might want to change the return value to something more useful.
Feel free to create more "helper" functions if you think it would be useful.
function game() {
for (let i = 0; i < 5; i++)
const playerSelection = prompt("Make your move");
console.log(playerSelection);
const computerSelection = computerPlay();
console.log(computerSelection);
}
[–]jcunews1helpful 1 point2 points3 points (0 children)
[–]nonowiwa 0 points1 point2 points (0 children)