Today I again continued with the JavaScript course by SuperSimpleDev and learned about JS objects. how interesting that concole.log()
is actually just a method.
Anyways, below my code again, which is an updated rock-paper-scissors game from last time, this time with a score object 😄
My Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rock Paper Scissors</title>
</head>
<body>
<p>Rock Paper Scissors</p>
<button onclick="
playGame('rock')
">Rock</button>
<button onclick="
playGame('paper');
">Paper</button>
<button onclick="
playGame('scissors');
">Scissors</button>
<button onclick="
score.wins = 0;
score.losses = 0;
score.ties = 0;
">Reset Score</button>
<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++;
} else if (result === 'You lose.') {
score.losses++;
} else if (result === 'Tie.') {
score.ties++;
}
console.log(`You picked ${playerMove}. Computer picked ${computerMove}. ${result}`)
console.log(`Wins: ${score.wins}, Losses: ${score.losses}, Ties: ${score.losses}`)
}
function pickComputerMove() {
let computerMove = '';
const randomNumber = Math.random();
if (randomNumber >= 0 && randomNumber < 1 / 3) {
computerMove = 'rock';
} else if (randomNumber >= 1 / 3 && randomNumber < 2 / 3) {
computerMove = 'paper';
} else {
computerMove = 'scissors';
};
return computerMove;
}
</script>
</body>
</html>