Javascript Rock Paper Scissors

hello everyone,

I am learning javascript and I have to create rock paper scissors game, at the end of the project I should create a cheat which is when I enter bomb I should directly win, I have created the conditions but every time it returns undefined…

here is my code

[codebyte]
const getUserChoice = userInput =>{
userInput=userInput.toLowerCase();
if (userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’){
return userInput;
} if (userInput===‘bomb’){
return ‘you knew the cheat sneaky’;
}
else {
return ‘you entered the wrong choice Sir’;
}
};

function getComputerChoice(){
let randomNumber=Math.floor(Math.random()*3);
switch (randomNumber){
case 0:
return ‘rock’;
case 1:
return ‘paper’;
case 2:
return ‘scissors’;
}
}

function determineWinner(userChoice,computerChoice){
if (userChoice===computerChoice){
return ‘the game was a tie’;
}
if ( userChoice===‘bomb’){
return ‘you win’;
}

if (userChoice===‘rock’){
if (computerChoice===‘paper’){
return ‘the computer has won’;
} else {
return ‘the user has won’;
}
}
if (userChoice===‘paper’){
if (computerChoice===‘scissors’){
return ‘the computer has won’;
} else {
return ‘you won’;
}
}
if (userChoice===‘scissors’){
if (computerChoice===‘rock’){
return ‘the computer has won’;
} else {
return ‘you won’;
}
}
}

function playGame(){
let userChoice=getUserChoice(‘rock’);
let computerChoice=getComputerChoice();
console.log('your choice is: '+userChoice);
console.log('the computer Choice is: '+computerChoice);
console.log(determineWinner(userChoice,computerChoice));

}
playGame();

thank you so much

Hello @alihisham01, welcome to the forums! When the user enters bomb, there is nothing in determineWinner to deal with it. Look through the determineWinner function. There’s logic for if the userChoice and computerChoice are equal. There’s logic for rock, paper, scissors, but there’s nothing to say what happens when userChoice="bomb".


If the user’s choice is bomb, then the compiler goes through determineWinner, and doesn’t reach any of the return statements in the if blocks. Since there’s no return outside of the if blocks (still inside the function), then the function doesn’t return anything. In JavaScript, the default return value is undefined.

I hope this helps!