const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput ==='paper' || userInput === 'scissors' || userInput === 'bomb') {
return userInput;
} else {
console.log('error, please enter a rock paper or sciscors!');
};
};
// console.log(getUserChoice('rock'));
const getComputerChoice = () => {
const randomNumber = Math.floor(Math.random() * 3);
console.log(randomNumber);
switch (randomNumber) {
case 0 :
return 'rock'
break;
case 1 :
return 'paper'
break;
case 2 :
return 'scissors'
break;
}
if (randomNumber === 0) {
return 'rock';
} else if (randomNumber === 1) {
return 'paper';
} else if (randomNumber === 2) {
return 'sciscors';
} //else {
//return 'error occured';
//}
};
function determineWinner(userChoice, computerChoice) {
if (userChoice === computerChoice) {
return 'game is tied!'
}if (userChoice === 'bomb') {
return 'You won!';
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won';
} else {
return 'The user won!';
}
} if (userChoice === 'paper'){
if (computerChoice === 'scissors') {
return 'Computer won!';
} else {
return 'You won!';
}
} if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'Computer won!';
} else {
return 'User won!';
}
}
}
//console.log(determineWinner('paper', 'scissors'));
function playGame () {
let userChoice = getUserChoice('rock')
let computerChoice = getComputerChoice()
console.log(userChoice);
console.log(computerChoice);
console.log(determineWinner(userChoice, computerChoice));
}
playGame();
This is my task i hope its beneficial