Closed Nalini1998 closed 1 year ago
// @Meow.Nalini98
const getUserChoice = (userInput) => {
userInput=userInput.toLowerCase();
if (userInput==="rock" || userInput==="paper" || userInput==="scissors") {
return userInput;
} else {
console.log('Error: Please input again!')
}
}
// console.log(getUserChoice('scissors'))
const getComputerChoice = () => {
const randomNumber = Math.floor(Math.random()*3);
switch(randomNumber) {
case 0:
return "rock"
case 1:
return "paper"
case 2:
return "scissors"
}
};
// console.log(getComputerChoice())
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return 'The game is a tie!';
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return "That's a pity! The computer won!"
} else {
return "Congrats! You won!"
}
}
if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return "That's a pity! The computer won!"
} else {
return "Congrats! You won!"
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return "That's a pity! The computer won!"
} else {
return "Congrats! You won!"
}
}
}
const playGame = () => {
const userChoice = getUserChoice('rock');
const computerChoice = getComputerChoice();
console.log('You threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
}
playGame()
Reference code was put at the comment below
Rock paper scissors Project is a classic two players game. Each player chooses either rock, paper, or scissors. The items are compared, and whichever player chooses the more powerful item wins.
The possible outcomes are:
Our code will break the game into four parts: