Added ability to play rounds, requires refreshing
This commit is contained in:
parent
4b5f653c2f
commit
68e06d552f
34
index.js
34
index.js
|
@ -4,4 +4,36 @@ function getComputerChoice() {
|
|||
return choices[Math.floor(Math.random() * 3)];
|
||||
}
|
||||
|
||||
console.log(getComputerChoice());
|
||||
function playRound(playerChoice, computerChoice) {
|
||||
let playerChoiceCapital = playerChoice.slice(0,1).toUpperCase() + playerChoice.slice(1,playerChoice.length)
|
||||
let computerChoiceCapital = computerChoice.slice(0,1).toUpperCase() + computerChoice.slice(1,computerChoice.length)
|
||||
|
||||
if (playerChoice == computerChoice) {
|
||||
return `It's a tie! ${playerChoiceCapital} vs ${computerChoiceCapital}.`;
|
||||
} else if ((playerChoice == "rock" && computerChoice == "paper")
|
||||
|| (playerChoice == "paper" && computerChoice == "scissors")
|
||||
|| (playerChoice == "scissors" && computerChoice == "rock")) {
|
||||
return `You lost! ${computerChoiceCapital} beats ${playerChoiceCapital}.`;
|
||||
} else if ((playerChoice == "rock" && computerChoice == "scissors")
|
||||
|| (playerChoice == "paper" && computerChoice == "rock")
|
||||
|| (playerChoice == "scissors" && computerChoice == "paper")) {
|
||||
return `You won! ${playerChoiceCapital} beats ${computerChoiceCapital}.`;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let correctOption = false
|
||||
let playerChoice;
|
||||
|
||||
while (correctOption == false) {
|
||||
playerChoice = prompt("What do you choose, 'rock', 'paper' or 'scissors'?").toLowerCase().trim();
|
||||
|
||||
choices.forEach(element => {
|
||||
if (playerChoice == element) {
|
||||
correctOption = true
|
||||
return;
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
console.log(playRound(playerChoice, getComputerChoice()));
|
Loading…
Reference in New Issue