Rock, Paper, Scissors Project Task 14

I have typed in the exact code for this project and everything is right up until the task 14: If a user types ‘bomb’ as their choice. This is my code.

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
 if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' || userInput === 'bomb') {
   return userInput;
 } else {
  console.log ('Error, please type: rock, paper, or scissors.');
   }
}
const getComputerChoice = () => {
  const randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
    case 0:
      return 'rock';
    case 1: 
      return 'paper';
    case 2:
      return 'scissors';
  }
  
  if (userChoice === 'bomb')
};

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice){
    return 'The game is a tie!';
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
   return 'Sorry, computer, won!';   
    } else { 
      return 'Congratulations, you won!';
    }
  }
  
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'Sorry, computer won!'
    } else {
      return 'Congratulations, you won!';
    }
  }
  
  if (userChoice === 'bomb') {
    return 'Congratulations, you won!';
  }
  };



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

playGame()

The error i get is a syntax error with }; every time and I don’t know what I am doing wrong. It looks right to me. Please help. Thank you.

I am not sure which one is the problem here, but I have two solutions.

Here may be a problem. There are two different type of quotes. I am not sure if this is the problem.

Here is the problem that, at least, my compiler doesn’t find the ending }. Maybe it is from the first problem. Test it out, maybe that is the problem. Please reply if it worked or not.

Edit: Also, watch this out:

Maybe the if attribute searches for {} and gets ones of other function. In my compiler although looks clean. Test this. In rest, the code is pretty clean.

Hello, @script1562005252, and welcome to the forums.

This doesn’t look quite right, does it?

You handle the user choosing ‘bomb’ here:

When I initially tried to run your code, I received the same syntax error. The error message includes a line number. That’s what led me to the first line of your code that I quoted. Error messages are quite useful. :wink:

One last tip/request: Please review this topic for future posts. How do I format code in my posts?

Ok, I got rid of the code at line 21 if (userChoice === ‘bomb’). I still have a syntax error. }); This is my new code. I can’t figure out what I’m doing wrong still I’ve tried a lot of ways to fix it and I’m still stuck.

const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === ‘rock’ || userInput === ‘scissors’ || userInput === ‘paper’ || userInput === ‘bomb’) {
return userInput;
} else {
console.log (‘Error, please type: rock, paper, or scissors.’);
}
}
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 ‘Sorry, computer, won!’;
} else {
return ‘Congratulations, you won!’;
}
}

if (userChoice === ‘paper’) {
if (computerChoice === ‘scissors’) {
return ‘Sorry, computer won!’
} else {
return ‘Congratulations, you won!’;
}
}

if (userChoice === ‘bomb’) {
return ‘Congratulations, you won!’;
}
}

};

const playGame = () => {
const userChoice = getUserChoice(‘bomb’);
const computerChoice = getComputerChoice();
console.log('You threw: ’ + userChoice);
console.log('The computer threw: ’ + computerChoice);

console.log(determineWinner(userChoice, computerChoice));
};

playGame()

Code that isn’t formatted is difficult to read. I’ll happily take a look at your code, if you’ll format it by placing it between triple back ticks:

image

Here is a link to my code

Remember that every block of code that begins with { must end with }. Check here:

const getComputerChoice = () => { //opening brace
  const randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) { //opening brace
    case 0:
      return 'rock';
    case 1: 
      return 'paper';
    case 2:
      return 'scissors';
 //missing closing braces

Then here:

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice){
    return 'The game is a tie!';
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
   return 'Sorry, computer, won!';   
    } else { 
      return 'Congratulations, you won!';
    }
  }
  
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'Sorry, computer won!'
    } else {
      return 'Congratulations, you won!';
    }
  }
  
  if (userChoice === 'bomb') {
    return 'Congratulations, you won!';
  }
}
  
  }; //what block does this belong to?

Other than the issues with the curly braces, It appears you are missing code to handle the user choosing scissors. :wink:

Some random thoughts and a bit of perspective:

Try to always insert complete things, so for example when you open a { then add } immediately before you start putting things between them.
And keep your indentation matching the braces at all time, it’s not something that you’d want to go back and fix later.
If your code is valid most of the time (runnable, not a construction site) then you can use the interpreter to your advantage to sanity check it, and if you’re doing this quite often then you also know that if there’s suddenly a syntax error then it’s something that you just did.

Text editors can help quite a lot as well, the text editor in codecademy’s environment doesn’t do much. They can for example constantly send the code to the interpreter/compiler for checking or reindent the code which makes missing braces really easy to spot as the indentation won’t match what you expected you might for example get things like

}
}

Which would suggest there’s one too many closing braces as otherwise it’d look like:

  }
}

All these if-statements that this exercise has you do are … less than ideal, you’d generally want to encode the rules in some non-nested and minimal way and then write code which works out how the comparisons need to happen… For example:

Bomb     `beats` _        = True
Rock     `beats` Scissors = True
Scissors `beats` Paper    = True
Paper    `beats` Rock     = True
_        `beats` _        = False

determineWinner a b | a == b      = "Tie!"
                    | a `beats` b = "Player wins!"
                    | otherwise   = "Computer wins, boo."

(Not javascript. Can’t even do this in javascript. But. You can do things that are similar enough, ie, list/declare rules, and then compare the input to the rules.)

But. You should definitely be able to handle the complexity of all those nested if-statements so it’s still very valuable exercise.