const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if(userInput === 'rock' || userInput === 'scissors' || userInput === 'paper') {
return userInput;
} else {
console.log('Error!');
}
}
const getComputerChoice = () => {
randomNumber = Math.floor(Math.random() * 3);
switch(randomNumber) {
case 0:
return 'rock';
break;
case 1:
return 'scissors';
break;
case 2:
return 'paper';
break;
}
}
const determineWinner = (userChoice, computerChoice) => {
if(userChoice === computerChoice) {
return 'Game is tied!';
} else if(userChoice === 'rock' && computerChoice === 'scissors') {
return 'User wins!';
} else if(userChoice === 'scissors' && computerChoice === 'rock') {
return 'Computer wins!';
} else if(userChoice === 'scissors' && computerChoice === 'paper') {
return 'User wins!';
} else if(userChoice === 'paper' && computerChoice === 'scissors') {
return 'Computer wins!';
} else if(userChoice === 'paper' && computerChoice === 'rock') {
return 'User wins!';
} else if(userChoice === 'rock' && computerChoice === 'paper') {
return 'Computer wins!';
}
}
function playGame() {
let userChoice = getUserChoice('paper');
console.log(userChoice);
let computerChoice = getComputerChoice('paper');
console.log(computerChoice);
console.log(determineWinner(userChoice, computerChoice))
}
playGame();
Is it okay to write determineWinner function this way:
const determineWinner = (userChoice, computerChoice) => {
if(userChoice === computerChoice) {
return 'Game is tied!';
} else if(userChoice === 'rock' && computerChoice === 'scissors') {
return 'User wins!';
} else if(userChoice === 'scissors' && computerChoice === 'rock') {
return 'Computer wins!';
} else if(userChoice === 'scissors' && computerChoice === 'paper') {
return 'User wins!';
} else if(userChoice === 'paper' && computerChoice === 'scissors') {
return 'Computer wins!';
} else if(userChoice === 'paper' && computerChoice === 'rock') {
return 'User wins!';
} else if(userChoice === 'rock' && computerChoice === 'paper') {
return 'Computer wins!';
}
}
The guy in the video wrote it in a different way, but mine does the same thingβ¦
Thank you!