Rock, Paper, Scissors Project

Hi Guys,

I am doing this Rock, Paper, Scissors Project as given in the link
https://www.codecademy.com/courses/introduction-to-javascript/projects/rock-paper-scissors-javascript?action=resume_content_item

when there is a tie, I also get ‘The Computer Won’ returned. Not sure why it’s not returned in the video in ‘Get Help’ section. The issue is when there is a tie, it goes inside the loop of - > userChoice = ‘Paper’ and prints the else return statement.

const getUserChoice = userInput =>{
  
  userInput = userInput.toLowerCase();
  
  if (userInput === 'rock'|| userInput === 'paper' || userInput === 'scissors'){
    return userInput;
  }
  else console.log(`${userInput} is not a valid input`);
  
}

// Get computer to make a choice
const getComputerChoice = () =>{
 let value = Math.floor(Math.random() * 3);
  switch(value){
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    default:
      return 'scissors'
  }    
}

// Creating a function to determine the winner
const determineWinner = (userChoice,computerChoice)=>{
  
  if (userChoice === computerChoice){
    console.log('The game is a tie');    
  }
  
 if (userChoice === 'rock'){
   if (computerChoice === 'paper'){
     return"The computer won!";
   }
   else  {
     return "The User Won";
 }
 }
  
  if (userChoice === 'paper'){
    if (computerChoice === 'rock')
      {
        return "The user won";}
    else {
      return "The computer won!";
         }
  }
  
  if (userChoice === 'scissors'){
    if (computerChoice === 'rock'){
      return "The computer won!";
    }
    else  {
      return "The user won";
    }
  }
  
};

// When there is a tie
console.log(determineWinner('paper', 'paper'));

Output is displayed as
The game is a tie
The computer won!

how to print the correct result with just one output “The game is a tie” ? Am I doing something wrong ?

Thanks

Notice that program is logging a tie and returning all the other results? That may have a bearing on the problem.

Yes it returns the else statement when userChoice ===‘Paper’. Not sure how to prevent it from returning else block when there is a tie.

The same is the case when there is a tie for ‘rock’ and ‘scissors’.

A possible reason maybe because when you state if the user won, you wrote:

 if (userChoice === 'rock'){
   if (computerChoice === 'paper'){
     return"The computer won!";
   }

This may create some problems in the code, due to the over-complicated nesting. Instead, if you want an outcome to happen if two things happen, try using:

if (var1 === 'string1' && var2 === 'string1'){
//code here
}

In other words, try using the and (&&), instead of nesting.

Consider this code:

if (var1 === var2){
return 'string1'
}else{
return 'string2'

If the ‘if’ statement is not true, then no matter what else happens, it will return string2. Avoid using else when you could just use else if; it makes the code easier to read through again, and debug-I find anyway!

Also, do you think it would be better to use else if for the code where you write this:

if (userChoice === computerChoice){
    console.log('The game is a tie');    
  }
  
 if (userChoice === 'rock'){
   if (computerChoice === 'paper'){
     return"The computer won!";
   }
   else  {
     return "The User Won";
 }
 }
  
  if (userChoice === 'paper'){
    if (computerChoice === 'rock')
      {
        return "The user won";}
    else {
      return "The computer won!";
         }
  }
  
  if (userChoice === 'scissors'){
    if (computerChoice === 'rock'){
      return "The computer won!";
    }
    else  {
      return "The user won";
    }
  }
  
};

instead of if…else?
Currently, I think your code will evaluate all the if statements that apply. For example, say the user picked ‘paper’ and the computer picked ‘paper’. In your code, this bit here:

if (userChoice === 'paper'){
    if (computerChoice === 'rock')
      {
        return "The user won";}
    else {
      return "The computer won!";
         }
  }

states that if the user picked ‘paper’ and the computer picked ‘rock’ print ‘The user won!’ However, it then says if the user didn’t pick ‘paper’ AND the computer picked ‘rock’, print ‘The computer won!’. This means that in our scenario where both picked ‘paper’ the code will evaluate the first if"


  if (userChoice === computerChoice){
    console.log('The game is a tie');    
  }

followed by all the other if statements. If you use else…if, I don’t think that would happen.
Sorry for the long winded reply!

Thanks I tried both the options + listing out each condition individually. All the options it’s printing correct output but with ‘undefined’ as well

  1. if (var1 === 'string1' && var2 === 'string1'){ //code here }

The below is the actual code

const getUserChoice = userInput =>{

  userInput = userInput.toLowerCase();

  if (userInput === 'rock'|| userInput === 'paper' || userInput === 'scissors'){
    return userInput;
  }
  else console.log(`${userInput} is not a valid input`);

}
// console.log(getUserChoice('paper')); /*to test what's priting from the above function*/

// Get computer to make a choice
const getComputerChoice = () =>{
 let value = Math.floor(Math.random() * 3);
  switch(value){
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    default:
      return 'scissors'
  }
}
// console.log(getComputerChoice());

// Creating a function to determine the winner
const determineWinner = (userChoice,computerChoice)=>{

  if (userChoice === computerChoice){
    console.log('The game is a tie');
  }

 if (userChoice === 'rock' && computerChoice === 'paper'){
        return"The computer won!";
   }

  if (userChoice === 'rock' && computerChoice === 'scissors'){
        return"The user won!";
   }

  if (userChoice === 'paper' && computerChoice === 'rock'){
        return"The user won!";
   }
  if (userChoice === 'paper' && computerChoice === 'scissors'){
        return"The computer won!";
   }
  if (userChoice === 'scissors' && computerChoice === 'rock'){
        return"The computer won!";
   }
  if (userChoice === 'scissors' && computerChoice === 'paper'){
        return"The user won!";
  }

};

// getUserChoice('rock');
console.log(determineWinner('rock', 'rock'));

  1. using else if for the code instead of just using else.

This is the actual code

const getUserChoice = userInput =>{
  
  userInput = userInput.toLowerCase();
  
  if (userInput === 'rock'|| userInput === 'paper' || userInput === 'scissors'){
    return userInput;
  }
  else console.log(`${userInput} is not a valid input`);
  
}
// console.log(getUserChoice('paper')); /*to test what's priting from the above function*/

// Get computer to make a choice
const getComputerChoice = () =>{
 let value = Math.floor(Math.random() * 3);
  switch(value){
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    default:
      return 'scissors'
  }    
}
// console.log(getComputerChoice());

// Creating a function to determine the winner
const determineWinner = (userChoice,computerChoice)=>{
  
  if (userChoice === computerChoice){
    console.log('The game is a tie');    
  }
  
 if (userChoice === 'rock'){
   if (computerChoice === 'paper'){
     return"The computer won!";
   }
   else if(computerChoice === 'scissors'){
     return "The User Won";}
 }
  
 if (userChoice === 'paper'){
    if (computerChoice === 'rock')
      {
        return "The user won";}
    else if (computerChoice === 'scissors'){
      return "The computer won!";
         }
  }
  
  if (userChoice === 'scissors'){
    if (computerChoice === 'rock'){
      return "The computer won!";
    }
    else if (computerChoice === 'paper') {
      return "The user won";
    }
  }
  
};

// getUserChoice('rock');
console.log(determineWinner('rock', 'rock'));
  1. Writing each condition of both the variables individually.
const getUserChoice = userInput =>{

  userInput = userInput.toLowerCase();

  if (userInput === 'rock'|| userInput === 'paper' || userInput === 'scissors'){
    return userInput;
  }
  else console.log(`${userInput} is not a valid input`);

}
// console.log(getUserChoice('paper')); /*to test what's priting from the above function*/

// Get computer to make a choice
const getComputerChoice = () =>{
 let value = Math.floor(Math.random() * 3);
  switch(value){
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    default:
      return 'scissors'
  }
}
// console.log(getComputerChoice());

// Creating a function to determine the winner
const determineWinner = (userChoice,computerChoice)=>{

  if (userChoice === computerChoice){
    console.log('The game is a tie');
  }

 if (userChoice === 'rock' && computerChoice === 'paper'){
        return"The computer won!";
   }

  if (userChoice === 'rock' && computerChoice === 'scissors'){
        return"The user won!";
   }

  if (userChoice === 'paper' && computerChoice === 'rock'){
        return"The user won!";
   }
  if (userChoice === 'paper' && computerChoice === 'scissors'){
        return"The computer won!";
   }
  if (userChoice === 'scissors' && computerChoice === 'rock'){
        return"The computer won!";
   }
  if (userChoice === 'scissors' && computerChoice === 'paper'){
        return"The user won!";
  }

};

// getUserChoice('rock');
console.log(determineWinner('rock', 'rock'));

All the above options is giving output

The game is a tie
undefined

I figured out the best way to do the code is for every “else if” after which we put “else” for tie, then it’s returning correct results without ‘undefined’ in output.
The below code worked !!

const getUserChoice = userInput =>{
  
  userInput = userInput.toLowerCase();
  
  if (userInput === 'rock'|| userInput === 'paper' || userInput === 'scissors'){
    return userInput;
  }
  else console.log(`${userInput} is not a valid input`);
  
}
// console.log(getUserChoice('paper')); /*to test what's priting from the above function*/

// Get computer to make a choice
const getComputerChoice = () =>{
 let value = Math.floor(Math.random() * 3);
  switch(value){
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    default:
      return 'scissors'
  }    
}
// console.log(getComputerChoice());

// Creating a function to determine the winner
const determineWinner = (userChoice,computerChoice)=>{
  
 if (userChoice === 'rock'){
   if (computerChoice === 'paper'){
     return"The computer won!";
   }
   else if(computerChoice === 'scissors'){
     return "The User Won";}
   else {return "The game is a tie";}
 }
  
 if (userChoice === 'paper'){
    if (computerChoice === 'rock')
      {
        return "The user won";}
    else if (computerChoice === 'scissors'){
      return "The computer won!";
         }
   else {return "The game is a tie";}
  }
  
  if (userChoice === 'scissors'){
    if (computerChoice === 'rock'){
      return "The computer won!";
    }
    else if (computerChoice === 'paper') {
      return "The user won";
    }
    else {return "The game is a tie";}
  }
  
};

// getUserChoice('rock');
console.log(determineWinner('rock', 'rock'));

Thanks for all your help ! :smiley:

actually, you should check for a tie first:

if (userChoice === computerChoice){
   // return its a tie
}

With that handled at the beginning of your determineWinner function, we can simplify the other comparisons, rock could now simply be:

if (userChoice === 'rock'){
   if (computerChoice === 'paper'){
     return"The computer won!";
   }
   else {
     return "The User Won";
   }
}

this simplifies conditions and eliminates repetitive code.

That’s what exactly what I did in my first attempt. But when there is a tie, it also outputs ‘Undefined’ with the correct output. If you look at the first code, I have evaluated the tie condition first in the determineWinner function.

except you use log instead of return:

    console.log('The game is a tie');    

now your function doesn’t return anything, so this log:

console.log(determineWinner('paper', 'paper'));

gives undefined

if you simply use return to return the its a tie string, its all good.

Awesome !! Thanks. This worked.

so for every function we define, we need to have a “return” statement ? without which the output would throw ‘undefined’ always ?

Consider how + works
where does the result come out?

If you have an array of numbers that you wish to sum up so that you could print the final sum… Imagine if + was printing a bunch of stuff while you were doing so, wouldn’t that be quite rude? If you ask for a result, then you want the result, what you don’t want is to have a bunch of other things happening on the side that you didn’t ask for.

that depends on if you log the returned result like you do here:

console.log(determineWinner('paper', 'paper'));
1 Like

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.