Rock, papers, scissors challenge - final result 'undeifined'


#1

Hi all,
Can somebody explain me why my code return ‘undefined’?
Where am i mystaken?
Link to the challenge - https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/learn-javascript-functions/projects/rock-paper-scissors


const getUserChoice = userInput => {
  	let choice = userInput.toLowerCase();
  	return choice === 'rock' || choice === 'paper' || choice === 'scissors' ? `You choose ${choice}` : console.log("You didn't choose the right element");
};

const getComputerChoice = () => {
  	let num = Math.floor(Math.random() * 3);
  	let element = '';	
  	switch(num) {
        case 0:
      				element +='rock';
          		break;
        case 1:
      				element +='paper';
          		break;
        case 2:
      				element +='scissors';
          		break;
      };
  		return element;
};

function determineWinner(userChoice, computerChoice) {
  		if (userChoice === computerChoice) {
        		return 'Game is tie';
      } 
  		else if (userChoice === 'rock') {
        		if (computerChoice === 'paper') {
              	return 'Computer wins the game';
            } else {
              	return 'User wins the game';
            }
      }
  		else if (userChoice === 'paper') {
        		if (computerChoice === 'scissors') {
              	return 'Computer wins the game';
            } else {
              	return 'User wins the game';
            }
      }
  		else if (userChoice === 'scissors') {
        		if (computerChoice === 'rock') {
              	return 'Computer wins the game';
            } else {
              	return 'User wins the game';
            }
      }
}

function playGame() {
      let userChoice = getUserChoice('rock');
      let computerChoice = getComputerChoice();
      determineWinner(userChoice, computerChoice);
}

playGame();

#2

undefined is the “default” returned value, so none of the condition seem to be true. Lets inspect the parameter values:

function determineWinner(userChoice, computerChoice) {
    console.log(userChoice);
    console.log(computerChoice);

I see a problem, you too?


#3

Not quite. undefined is how JavaScript interprets no return value. playGame() has no return.


#4

true, but the problem for OP is caused by determineWinner not returning a value


#5

Cannot see any problem :frowning: Can you show me where am i mystaken and why the code doesn’t print anything?


#6

But it is returning a value. The OP is just not logging it.


#7

i saved your code here:

https://jsbin.com/xotefayelu/edit?html,js,console

with the code i suggested i can see that userChoice contains a wrong value.

yea, undefined because there is no return value. He is also not logging it, true, but i would have gotten to that later.


#8

userChoice takes the value from the data in playGame() function. I cannot see anything wrong. Just determineWinner() function inside playGame() function doesn’t give the result;

Can you fix the code to see exactly what is wrong?


#9

why playGame() doesn’t return anything?


#10
 > playGame()
   You choose rock
   paper
   Fail

It doesn’t have to. It just runs and ends. The console will echo undefined. You problem is hinted at by @stetim94. Look above at what your getUserChoice returns.


#11

Thanks. I just saw where the problem is. I need to modify my getUserChoice expression.


#12
return choice === 'rock' || choice === 'paper' || choice === 'scissors' ? `You choose ${choice}` : console.log("You didn't choose the right element");

Consider ways to simplify the conditional…

    return "rockpaperscissors".includes(choice) ? choice : false;
    userChoice = getUserChoice('stone');
    if (userChoice) {
        getComputerChoice();
        console.log(determineWinner(userChoice, computerChoice));
    } else {
        console.log("You didn't choose the right element");
    }

#13

You are amazing guys, thanks a lot.
I used switch statement instead:

const getUserChoice = userInput => {
let choice = userInput.toLowerCase();
switch(choice) {
case ‘rock’:
case ‘paper’:
case ‘scissors’:
case ‘bomb’:
return choice;
break;
default:
return “Please, choose rock, paper or scissors”;
break;
}
};


#14

Beware code bloat…

const getComputerChoice = () => {
  	let num = Math.floor(Math.random() * 3);
  	let element = '';	
  	switch(num) {
        case 0:
      				element +='rock';
          		break;
        case 1:
      				element +='paper';
          		break;
        case 2:
      				element +='scissors';
          		break;
      };
  		return element;
};

The switch can be written in simplest form, as follows…

    switch(Math.floor(Math.random() * 3)) {
    case 1: return 'paper';
    case 2: return 'scissors';
    default: return 'rock'
    }

Or use an array literal…

    return ['rock', 'paper', 'scissors'][Math.floor(Math.random() * 3)];

#15

10x a lot. Maybe i need some rest because i cannot see the obvious.


#16

I feel really stupid :slight_smile:


#17

Don’t. You are expected to follow instructions, and cannot be expected to envision simple solutions when not fully equipped. We should never be embarrassed when learning something as difficult as programming. In time you will learn how to write code, get it to work (which means finding mistakes), then revise as needed to simplify. We never write the finished product out of the gate.

As Steven demonstrated, log your intermediate results so the data can be checked as the code runs.