Rock Paper Scissors [Exercise]
In this exercise we take user input and computer input to play rock, paper, scissor and decides who wins.
Solution Code:
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (userInput === "rock" || "paper" || "scissors") {
console.log(userInput);
} else {
console.log("Error");
}
};
function getComputerChoice() {
const randomNumber = Math.floor(Math.random() * 3);
if (randomNumber === 0) {
console.log("rock");
} else if (randomNumber === 1) {
console.log("paper");
} else {
console.log("scissor");
}
}
// console.log(getComputerChoice());
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
console.log("The game is a tie.");
}
if (userChoice === "rock") {
if (computerChoice === "paper") {
console.log("The computer won.");
} else {
console.log("The user won.");
}
}
if (userChoice === "paper") {
if (computerChoice === "rock") {
console.log("The user won.");
} else {
console.log("The computer won.");
}
}
if (userChoice === "scissors") {
if (computerChoice === "paper") {
console.log("The computer won.");
} else {
console.log("The user won.");
}
}
};
// console.log(determineWinner('scissor', 'rock'));
function playGame() {
let userChoice = getUserChoice("rock");
// console.log(userChoice);
let computerChoice = getComputerChoice();
// console.log(computerChoice);
// console.log(determineWinner(userChoice, computerChoice));
}
playGame(); // Lets play the game
Last updated