Javascript Functions: Project : Rock, Paper, Scissors

Hello all,

I am currently doing Full-stuck Engineer carrerer path.

In JS function rock, paper, scissors project, I am getting a wrong response from a written code.

Could you please review my code and find an mistake?

The code is below:

const getUserChoice=userInput => {
userInput=userInput.toLowerCase();
if (userInput===‘rock’ || userInput===‘paper’ || userInput===‘scissors’ || userInput===‘bomb’){
return console.log(userInput);}
else {return console.log(‘Wrong choice is chosen’);}
};

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

const determineWinner=(userChoice,computerChoice)=>{
if (userChoice===computerChoice){
return ‘The game is a tie’;}
if (userChoice===‘rock’) {
if (computerChoice===‘paper’){
return ‘Computer has won’;}
else {return ‘User has won’;}}
if (userChoice===‘paper’) {
if (computerChoice===‘scissors’){
return ‘Computer has won’;}
else {return ‘User has won’;}}
if (userChoice===‘scissors’){
if (computerChoice===‘rock’){
return ‘Computer has won’;}
else {return ‘User has won’;}}
if (userChoice===‘bomb’){
return ‘Congratulations you won’;}
};

const playGame = () => {
const userChoice = getUserChoice(‘bomb’);
const computerChoice = getComputerChoice();
console.log('You made ’ + userChoice);
console.log(Computer made ${computerChoice});
console.log(determineWinner(userChoice,computerChoice));
};

playGame();

After running the code I am getting this response.

bomb
You made undefined
Computer made scissors
undefined

It says that the parameter for userChoice is undefined. I checked every return for userInput, but everthying looks ok.

1-question is: Why does it return a ‘bomb’
2-question is: Why am I getting undefined?

That would be at the top of my heap of if else’s. It’s not a default.

Change that, format your code sample and we might find more little improvements.

1 Like

Thank you for your advice. I wasn’t clear about my question. I will update it now. Sorry for a confusion.

Hey Roy,

I fixed my problem by looking answers of previous questions about ‘undefined’. At the function getUserChoice I eraised extra console.log’s.

Now the code looks like this and works well.

const getUserChoice=userInput => {
userInput=userInput.toLowerCase();
if (userInput===‘rock’ || userInput===‘paper’ || userInput===‘scissors’ || userInput===‘bomb’){
return userInput;}
else {return ‘Wrong choice is chosen’;}
};

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

const determineWinner=(userChoice,computerChoice)=>{
if (userChoice===computerChoice){
return ‘The game is a tie’;}
if (userChoice===‘rock’) {
if (computerChoice===‘paper’){
return ‘Computer has won’;}
else {return ‘User has won’;}}
if (userChoice===‘paper’) {
if (computerChoice===‘scissors’){
return ‘Computer has won’;}
else {return ‘User has won’;}}
if (userChoice===‘scissors’){
if (computerChoice===‘rock’){
return ‘Computer has won’;}
else {return ‘User has won’;}}
if (userChoice===‘bomb’){
return ‘Congratulations you won’;}
};

const playGame = () => {
const userChoice = getUserChoice(‘Paper’);
const computerChoice = getComputerChoice();
console.log('You made ’ + userChoice);
console.log(Computer made ${computerChoice});
console.log(determineWinner(userChoice,computerChoice));
};

playGame();

Thanks again Roy!

1 Like