Hi!
I’m back from a break now relearning js
got a a problem i cant sort out
At the Rock Paper Scissors project at the functions unit under the full stack engineer course
I’m mostly finished, before doing the 14th step though I wanna make sure if a user
inputs invalid option the game will only pop the error message
currently determine winner function continues although userChoice returns error msg
How do I make the program stop after delivering the error msg
without resorting to another else if statement in determineWinner();
to make it happen?
my code below:
//recieve user choice
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if( userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
return userInput;
}else{
console.log('Invalid selection,choose only rock paper scissors');
}
}
/*test function user
console.log(getUserChoice('paper')); */
//calculate computer choice
const getComputerChoice = () =>{
let computerChoice = Math.floor(Math.random()*3);
switch (computerChoice){
case 0:
return computerChoice='rock';
break;
case 1:
return computerChoice='paper';
break;
case 2:
return computerChoice='scissors';
break;
}
}
/* test function computer
console.log(getComputerChoice()); */
const determineWinner = (userChoice,computerChoice) =>{
if (userChoice === computerChoice) {
return 'The game is tied';
}else if ( userChoice === 'rock') {
if( computerChoice === 'paper'){
return 'The computer wins!';
}else{
return 'Player wins!';
}
}else if( userChoice ==='paper'){
if( computerChoice ==='rock'){
return 'Player wins!';
}else{
return 'The computer wins!';
}
}else{
if( computerChoice ==='rock'){
return 'The computer wins';
}else{
return 'Player wins!';
}
}
}
/*testing the function
determineWinner(getUserChoice('scissors'),getComputerChoice()); */
//starts up the game
const playGame = () => {
const userChoice=getUserChoice('NUMNUM');
const computerChoice=getComputerChoice();
console.log(`Player choose ${userChoice}.
Computer choose ${computerChoice}`);
console.log(determineWinner(userChoice,computerChoice));
};
playGame();
Thanks!