Added points and ditched console for alerts

This commit is contained in:
NetMan 2024-01-03 20:06:15 +01:00
parent 68e06d552f
commit 8781da040e
1 changed files with 37 additions and 15 deletions

View File

@ -9,31 +9,53 @@ function playRound(playerChoice, computerChoice) {
let computerChoiceCapital = computerChoice.slice(0,1).toUpperCase() + computerChoice.slice(1,computerChoice.length) let computerChoiceCapital = computerChoice.slice(0,1).toUpperCase() + computerChoice.slice(1,computerChoice.length)
if (playerChoice == computerChoice) { if (playerChoice == computerChoice) {
return `It's a tie! ${playerChoiceCapital} vs ${computerChoiceCapital}.`; return {string: `It's a tie! ${playerChoiceCapital} vs ${computerChoiceCapital}.`,
points: {player: true, computer: true}};
} else if ((playerChoice == "rock" && computerChoice == "paper") } else if ((playerChoice == "rock" && computerChoice == "paper")
|| (playerChoice == "paper" && computerChoice == "scissors") || (playerChoice == "paper" && computerChoice == "scissors")
|| (playerChoice == "scissors" && computerChoice == "rock")) { || (playerChoice == "scissors" && computerChoice == "rock")) {
return `You lost! ${computerChoiceCapital} beats ${playerChoiceCapital}.`; return {string: `You lost! ${computerChoiceCapital} beats ${playerChoiceCapital}.`,
points: {player: false, computer: true}};
} else if ((playerChoice == "rock" && computerChoice == "scissors") } else if ((playerChoice == "rock" && computerChoice == "scissors")
|| (playerChoice == "paper" && computerChoice == "rock") || (playerChoice == "paper" && computerChoice == "rock")
|| (playerChoice == "scissors" && computerChoice == "paper")) { || (playerChoice == "scissors" && computerChoice == "paper")) {
return `You won! ${playerChoiceCapital} beats ${computerChoiceCapital}.`; return {string: `You won! ${playerChoiceCapital} beats ${computerChoiceCapital}.`,
points: {player: true, computer: false}};
} }
} }
function game() {
let points = {
player: 0,
computer: 0,
}
let correctOption = false for (let i = 0; i < 5; i++) {
let playerChoice; let correctOption = false
let playerChoice;
while (correctOption == false) { while (correctOption == false) {
playerChoice = prompt("What do you choose, 'rock', 'paper' or 'scissors'?").toLowerCase().trim(); playerChoice = prompt("What do you choose, 'rock', 'paper' or 'scissors'?").toLowerCase().trim();
choices.forEach(element => { choices.forEach(element => {
if (playerChoice == element) { if (playerChoice == element) {
correctOption = true correctOption = true
return; return;
}
})
} }
})
let round = playRound(playerChoice, getComputerChoice());
if (round.points.player) {
points.player++;
}
if (round.points.computer) {
points.computer++;
}
let pointsString = `Points: ${points.player}:${points.computer}\n`;
alert(pointsString + round.string);
}
} }
console.log(playRound(playerChoice, getComputerChoice())); game();