Rock,paper,scissors: undefined when calling determineWinner


#1

Hello guys,

i’m having a lot of fun (re)learning JS, but I am stuck at the very end of this exercise. I’ve looked on the forums and it seems I’m the only one with this error.

Ok here’s my code:

  userInput=userInput.toLowerCase();
  	if(userInput=='rock' || userInput==='paper' || userInput==='scissors'){
      return userInput;
    }else{
      console.log('Wrong!');
      return 'You wrote this,idiot: '+userInput;
    }
};

const getComputerChoice=()=>{
  let randomNumber= Math.floor(Math.random()*3);
  
  switch(randomNumber){
    case 0:
      return 'Scissors';
      
    case 1:
      return 'Paper';
      
    case 2:
      return 'Rock';
      break;
  }
};

const determineWinner = (userChoice,computerChoice)=>{
 
  if(userChoice===computerChoice){
   return 'It\' a tie!';
  }
  
   if(userChoice==='Rock'){
    if(computerChoice==='Paper'){
      return 'Computer won!';
    }else if(computerChoice==='Scissors'){
      return 'User won!';
    }
  }
  
  if(userChoice==='Paper'){
    if(computerChoice==='Rock'){
      return 'User won!';
    }else if(computerChoice==='Scissors'){
      return 'Computer won!';
    }
  }
  
  if(userChoice==='Scissors'){
    if(computerChoice==='Rock'){
      return 'Computer won';
    }else if(computerChoice==='Paper'){
      return 'User won!';
    }
  }  
 
};

const playGame=()=>{
  
  let userChoice=getUserChoice('Rock');
  let computerChoice=getComputerChoice();
  
  
  console.log('You threw: ' + userChoice);
   console.log('The computer threw: ' + computerChoice);
   console.log(determineWinner(userChoice,computerChoice));
};

playGame();

So the two first console logs of playGame() are shown, but then, I get a third line with “undefined”.

Here’s the tricky part, when going back to the determineWinner function, and console logging (like in step 11) it works, but not when it goes through the playGame() function.

I’m really pulling the few hair i’ve got left on this error. I think there’s a mistake in the determineWinner function, but I can’t see where it could be.

Thanks a lot in advance for waving my hair and my week-end.

ps: i hope the code is well formated


#2
  if(userChoice==='Paper'){
    if(computerChoice==='Rock'){
      return 'User won!';
    }else if(computerChoice==='Scissors'){
      return 'Computer won!';
    }
  }

Too much logic for the requirements. Ties are already ruled out so if the computer is not Rock then it must be Scissors. A simple if…else is all this binary condition requires.


#3

Hello Mtf!
Yeah that’s what I thought, I removed the “else if” conditions to transform them into simple “else”.

Yet, I still have the “undefined” error upon calling the function inside “playGame()” :frowning:


#4

Can you paste in the output from the console?


#5

yep, here it is:
You threw: rock
The computer threw: Paper
undefined


#6

Sorry, should have asked for it earlier… Your revised code, please?


#7
const getUserChoice = userInput =>{
          userInput = 'rock';
		  userInput = userInput.toLowerCase();
  	  if (userInput==='rock' || userInput==='paper' || userInput==='scissors'){
		  
       return userInput;
    }else{
      return`Wrong!
      You wrote this,idiot: ` + userInput;
    };
}
getUserChoice('rock')
const getComputerChoice=()=>{
  let randomNumber= Math.floor(Math.random()*3);
  
  switch(randomNumber){
    case 0:
      return 'Scissors';
      break;
    case 1:
      return 'Paper';
      break;
    case 2:
      return 'Rock';
      break;
  };
}

const determineWinner = (userChoice,computerChoice)=>{
    userChoice = getUserChoice();
	computerChoice = getComputerChoice();
  if(userChoice===computerChoice){
   return 'It\'s a tie!';
  }
  
   else if(userChoice==='Rock'){
    if(computerChoice==='Paper'){
      return 'Computer won!';
    }
  }
  
  else if(userChoice==='Paper'){
    if(computerChoice==='Rock'){
      return 'User won!';
    }
  }
  
  else if(userChoice==='Scissors'){
    if(computerChoice==='Rock'){
      return 'Computer won';
    }
  } else {
	  return `Computer Won`
  }
 
}
  
  
  console.log('You threw: ' + getUserChoice());
  console.log('The computer threw: ' + getComputerChoice());
  console.log(determineWinner());

Yo have made lots of mistakes. So follow this, Then you’ll know what was wrong.


#9

hello there and thanks for the code.

I copied and pasted it and it works! Thanks!

So, comparing your code and mine, the biggest difference i can see is that you gave userInput a value at the start,which makes sense and I forgot to do so.

Now there’s only one problem with your code: even when I set this userInput to say, “paper” or “Paper” and the computer throws “Paper”,it says that the computer won, which is wrong.

I’ll work on this later on tonight, but many many many thanks for the help to you and mtf!

Edit: about the error I mentionned, it’s because for the computer, his choices are always with a capital letter. So I’ll be working on fixing this, and I’ll show you my code ^^

Second Edit:

Ok so…i’m definitely lost and no hope left lol

here’s my code:

const getUserChoice = userInput =>{
  userInput='rock';
  userInput=userInput.toLowerCase();
  	 if (userInput==='rock' || userInput==='paper' || userInput==='scissors'){
       return userInput;
    }else{
      return`Wrong! You wrote this,idiot: ` + userInput;
    };
};

const getComputerChoice=()=>{
  let randomNumber= Math.floor(Math.random()*3);
  
  switch(randomNumber){
    case 0:
      return 'Scissors'.toLowerCase();
      
    case 1:
      return 'Paper'.toLowerCase();
      
    case 2:
      return 'Rock'.toLowerCase();
      break;
  }
};

const determineWinner = (userChoice,computerChoice)=>{
  
  userChoice = getUserChoice();
	computerChoice = getComputerChoice();
  
  if (userChoice === computerChoice) {
  return 'It is a tie!';
}
  
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
    return 'The computer won!';
    } else {
    return 'You won!';
  }
  }
    
    if (userChoice === 'paper') {
      if (computerChoice === 'scissors') {
        return 'The computer won!';
      } else {
        return 'You won!';
      }
		}
    
    if (userChoice === 'scissors') {
  if (computerChoice === 'rock') {
    return 'The computer won!';
  } else {
    return 'You won!';
  }
}
    
}
 
const playGame = () => {
   const userChoice = getUserChoice('scissors');
   const computerChoice = getComputerChoice();
 
   console.log('You threw: ' + userChoice);
   console.log('The computer threw: ' + computerChoice);
   console.log(determineWinner(userChoice, computerChoice));
};

playGame();

Here, the results have no sense at all. For example, the last console log gave me:
You threw: rock
The computer threw: rock
You won!

In my conditions, there’s a if userChoice===computerChoice, it logs a tie, but it actually doesn’t here.

Now, for the tie, I have this:

You threw: rock
The computer threw: paper
It is a tie!

As you can see, it somehow gives random ties.

I also made put .toLowerCase(); in the switch condition, didn’t know where to put it.

I can almost feel that I’m not far from the correct answer, but I’ve actually gone bald in a matter of hours.

Edit 3:

I did it!!! I actually wasn’t correct in my conditions in the determineWinner function. Here’s the final (I hope) version, and it doesn’t send me weird results:

const getUserChoice = userInput =>{
  userInput=userInput.toLowerCase();
  	 if (userInput==='rock' || userInput==='paper' || userInput==='scissors'){
       return userInput;
    }else{
      return`Wrong! You wrote this,idiot: ` + userInput;
    };
};

const getComputerChoice=()=>{
  let randomNumber= Math.floor(Math.random()*3);
  
  switch(randomNumber){
    case 0:
      return 'Scissors'.toLowerCase();
      
    case 1:
      return 'Paper'.toLowerCase();
      
    case 2:
      return 'Rock'.toLowerCase();
      break;
  }
};

const determineWinner = (userChoice, computerChoice) => {
   if (userChoice === computerChoice) {
    return 'This is a tie!'; 
  } 
  else if (userChoice === 'rock') {
    if(computerChoice==='scissors'){
      return 'You won'; 
    }else{
      return 'Computer won'; 
    }
  } 
  else if (userChoice === 'paper'){
   if(computerChoice==='rock'){
      return 'You won'; 
    }else{
      return 'Computer won'; 
    }
  }
  else if (userChoice === 'scissors'){
   if(computerChoice==='paper'){
      return 'You won'; 
    }else{
      return 'Computer won'; 
    }
  }
}
 
const playGame = () => {
   const userChoice = getUserChoice('rock');
   const computerChoice = getComputerChoice();
 
   console.log('You threw: ' + userChoice);
   console.log('The computer threw: ' + computerChoice);
   console.log(determineWinner(userChoice, computerChoice));
};

playGame();

What i did:
Always make sure the user is the winner in the first condition of an else if block, then if it goes into the else condition, he has to be the loser of this round.

Plus, my last edited code wasn’t correct since I had no “if else” conditions.

Ok now I have to make a more practical “toLowerCase()” on the computerChoice string, get a prompt to work and i’ll grow my hair back.

thanks again everyone and sorry for the gazillions edits! I’m just so happy!


#10

Great!:smiley:
But one thing is that always use break after every case of switch statement & that’s a good programming habit… Best of luck…


#11

yep! I’ll add the break; right now.
Haven’t had the time today to revise my code, but will probably do it later this week, I want to keep going forward with the courses.

See you soon for more noob questions héhé