<p>Rock Paper Scissors</p>
</div>
<div>
<button onclick="
playGame('Rock');
">Rock</button>
<button onclick="
playGame('Paper');
">Paper</button>
<button onclick="
playGame('Scissors');
">Scissors</button>
</div>
<script>
const score = {
wins: 0,
losses: 0,
ties: 0
}
function playGame(playerMove){
const computerMove = pickComputerMove();
let result = '';
if (playerMove === 'Scissors'){
if(computerMove === 'Rock'){
result = ('You Lose');
} else if(computerMove === 'Paper'){
result = ('You Win');
} else if (computerMove === 'Scissors'){
result = ('Tie');
}
} else if(playerMove === 'Paper'){
if(computerMove === 'Rock'){
result = ('You Win');
} else if(computerMove === 'Paper'){
result = ('Tie');
} else if (computerMove === 'Scissors'){
result = ('You Lose');
}
}else if(playerMove === 'Rock'){
if(computerMove === 'Rock'){
result = ('Tie');
} else if(computerMove === 'Paper'){
result = ('You Lose');
} else if (computerMove === 'Scissors'){
result = ('You Win');
}
if(result === 'You Win'){
score.wins = score.wins + 1;
} else if(result === 'You Lose'){
score.losses = score.losses + 1;
}else if(result === 'Tie'){
score.ties = score.ties + 1;
}
}
alert(`You picked ${playerMove}.Computer picked ${computerMove}. ${result}
Wins: ${score.wins}, Losses: ${score.losses}, Ties: ${score.ties}`);
}
function pickComputerMove(){
const randomNumber = Math.random();
let computerMove = '';
if(randomNumber >= 0 && randomNumber < 1/3){
computerMove = ('Rock');
} else if(randomNumber >=1/3 && randomNumber <2/3){
computerMove = ('Paper');
} else if(randomNumber >= 2/3 && randomNumber <1){
computerMove = ('Scissors');
}
return computerMove;
//A return statement ends the function.
}
</script>
Rock-Paper-Scissors
- share this (step/exercise) url
- share “live” (codepn/repl/etc) link for this as well
happy coding
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.