Rock, Paper, Scissors Javascript Lesson

Hi there,

I am new to JavaScript (and programming as a whole). I have managed to write the code to how I believe it should be written, for it to work, however it keeps saying that I have undeclared variables/functions.

I have been spending the last 30 minutes looking through the ‘Hint’s’ section on the activity, and honestly looking back at it, the lesson doesn’t actually make sense, as they consistently change the names of the functions and variables.

Would someone be able to take a look at my code and amend where ive gone wrong.

Ive written the code exactly how the Codecademy lesson wants me to, but to no avail.

Here’s the GitHub link: https://gist.github.com/a3598a5febc2606f7a7742e930b1f280

Many thanks :slight_smile:

P.S. If anyone from Codecademy is reading this, and I am indeed correct in believing this lesson is not written properly… It is extremely disheartening to of spent time following the lesson, and then not to be able to achieve anything because im too confused by the instructions.

Hey Lewis,

At the bottom of your code, under the playGame function, pay attention to these two console.logs:

console.log('You threw: ' + userChoice());
console.log('The computer threw: ' + computerChoice());

Try removing the parenthesis attached to the userChoice and computerChoice, like so:

console.log('You threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);

I believe that is what’s causing the error, as userChoice and computerChoice are NOT functions by themselves, but rather they’re parameters of the function determineWinner. Try it out and let me know if it solves the problem you’re having :slight_smile:

On a side note, at the top of your code where you’re using switch statements, make sure to include the break keyword at the end of each code block, right after your return statement and a default statement at the end of the switch statement, like so:

switch (randomNumber) {
    case 0:
      return 'rock';
      break;
    case 1:
      return 'paper';
      break;
    case 2:
      return 'scissors';
      break;
    default:
      return 'Error!';
      break;
  }

This is good practice for writing a switch statement, as when it comes to checking whether conditions are true or false, they’re slightly different from an if/else statement.

In an if/else statement, the code will stop running the moment when a condition evaluates to true. In a switch statement, the code will continue to run through all of the statements to evaluate their conditions, regardless whether a condition has already been evaluated to true.

This is where the break keyword comes in. By inserting the break keyword, the code will stop evaluating the rest of the conditions the moment a condition is evaluated to true. If no conditions are evaluated to true, it will execute the condition inside the default statement. Think of the default statement as the equivalent to an else statement at the end of an if/else statement.

The code will still run fine, even if you don’t include the break keywords or a default statement at the end. But in the event, say, of a large program or website where there are thousands of lines of code to be executed, it could save up some time for the entire application/ website to run and in return, makes user experience much more pleasant :blush:

I hope that help clear things up for you. If not, my apologies in advance if my explanation is difficult to understand and confusing :grimacing: :bowing_man:

2 Likes

Hi there,

Thanks for the help, it all works now. I just have an issue with the determineWinner section now, as it isn’t actually changing the console log based on the answers either me or the computer give. I have attached a screenshot if it helps :slight_smile:

Any help is appriciated

hello, I was practicing the JavaScript’s ‘Rock, Paper Scissors’ project. I encountered a confusing concept. So, a section of my source code looked like this:

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'Whoa! that was close! it was a tie. let\'s try again...'
  }
  else{
    if (userChoice === 'rock') {
      if (computerChoice === 'paper'){
        return 'I beat you! better luck next time! hihi!'}
      else{
        return 'mahn! you are just lucky! let\'s try one more time...'
      }
    }
     else if (userChoice ==='paper') {
      if (computerChoice === 'scissors'){
        return 'I beat you! better luck next time! hihi!'}
      else{
        return 'mahn! you are just lucky! let\'s try one more time...'
      }
    }
    else (userChoice === 'scissors') {
      if (computerChoice === 'rock'){
        return 'I beat you! better luck next time! hihi!'}
      else{
        return 'mahn! you are just lucky! let\'s try one more time...'
      }
    }
  }
}
console.log(determineWinner('paper' , 'paper'));

i was bugged by a syntax error. I watched the walkthrough video and noticed that i had written ‘else if’ and ‘else’ statements instead of ‘if’.

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'Whoa! that was close! it was a tie. let\'s try again...'
  }
  else{
    if (userChoice === 'rock') {
      if (computerChoice === 'paper'){
        return 'I beat you! better luck next time! hihi!'}
      else{
        return 'mahn! you are just lucky! let\'s try one more time...'
      }
    }
     if (userChoice ==='paper') {
      if (computerChoice === 'scissors'){
        return 'I beat you! better luck next time! hihi!'}
      else{
        return 'mahn! you are just lucky! let\'s try one more time...'
      }
    }
    if (userChoice === 'scissors') {
      if (computerChoice === 'rock'){
        return 'I beat you! better luck next time! hihi!'}
      else{
        return 'mahn! you are just lucky! let\'s try one more time...'
      }
    }
  }
}
console.log(determineWinner('paper' , 'paper'));

Anyone, please help understand this. Thank you in advance

Your code is actually running as intended from what I can tell, however on line 59 you need to remove the ( ) from next to computerChoice, as it is a variable and not a function. Also remove the parameters from playGame, as you aren’t actually using these parameters in your function since you’re defining variables anyway. That should make everything output correctly.

Your code is for the most part fine, if written a little confusingly. Generally using the separate if’s just keeps the statements clearer and more readable allowing for better debugging, but your code does work aside from one issue.

else{
    if (userChoice === 'rock') {
      if (computerChoice === 'paper'){
        return 'I beat you! better luck next time! hihi!'}
      else{
        return 'mahn! you are just lucky! let\'s try one more time...'
      }
    }
     else if (userChoice ==='paper') {
      if (computerChoice === 'scissors'){
        return 'I beat you! better luck next time! hihi!'}
      else{
        return 'mahn! you are just lucky! let\'s try one more time...'
      }
    }
    else (userChoice === 'scissors') {
      if (computerChoice === 'rock'){
        return 'I beat you! better luck next time! hihi!'}
      else{
        return 'mahn! you are just lucky! let\'s try one more time...'
      }
    }

The problem is the else for userChoice === scissors. This should either be an else statement, meaning there is no conditional, or an else if statement with the conditional in the brackets you have currently. Else statements are used when you want the code inside to be used for every other case, in this case when the user does anything other than ‘rock’ or ‘paper’. This would be fine for this example, but for robustness I would just make this an else if and keep the condition.

1 Like

thank you @adamgaffney137183916.
If you don’t mind, please inform me how i should make my code less confusing. I would appreciate some positive criticism. Thank you in advance.

Honestly what I’d say is just don’t be afraid to use the whitespace. This is a personal thing though so if you think it looks okay then that’s alright, but I like putting the open bracket next to the function, and then the close bracket on it’s own line, so I can easily trace when functions have been opened and closed. That is more personal preference though so don’t feel bad about not doing it that way, it’s all about being readable.

I think the main thing was just having everything wrapped in the else statement. You could have made this just a chain of else ifs and you’d be okay. For the most part it looks very good though and you clearly understand the syntax well!

1 Like

@adamgaffney137183916 okay, thank you.

Hi there,

Can someone tell me what’s wrong in my code?
Here’s the GitHub link: https://gist.github.com/8ecddbfae6e0596802802a055cb18b79
Can someone correct it please?

Thanks a lot!

const determineWinner = (getUserChoice, getComputerChoice) => {if (getUserChoice === getComputerChoice) {return 'The game is a tie!';} 
if (getUserChoice === 'rock') {if (getComputerChoice === 'paper') {return 'Sorry, computer Won!';} else {return 'Congrats, you won!';}
if (getUserChoice === 'paper') {if (getComputerChoice === 'scissors'){return 'Sorry, computer Won!';} else {return 'Congrats, you Won!';}}
 if (getUserChoice === 'scissors') {if (getComputerChoice === 'rock'){return 'Sorry, computer Won!';} else {return 'Congrats, you Won!';}}
}

Your issue is in this section here. For the if (getUserChoice === 'rock') statement, you haven’t actually closed it! Just need to add an extra curly bracket to the end of that line, and you should be okay.