Help with Rock Paper Scissors Project

Can Someone Help me Please… Why did I get this Syntax Error?

Looks fine, could you provide the exercise url and copy paste your code to the forum? This way, I/we can run the code, simplifying debugging a lot :slight_smile:

You have an extra parenthesis here:

if(userChoice === ("paper")

First you have the opening ( for the if condition, but then you have another opening ( inside the condition for some reason?

If you still keep getting an error after fixing that then you should post your code here, like @stetim94 said.

Hope this helped! :slightly_smiling_face:

3 Likes

I wasn’t looking at the right line. Good catch :slight_smile:

2 Likes

When you say, provide the Exercise URL do you mean… copy the address from the address bar into the Box?

Hi thanks for pointing out the bracket before… Now the lower if statement is throwing up undefined… for paper scissors, but the other outcomes work fine… can you help?

const determineWinner = (userChoice,computerChoice) => {// + 1.1 Outer
 if(userChoice === computerChoice) { // Inner to Outer
return "the Game was a tie!"
 }; // 
 
if (userChoice === "rock") { // +2.1
  if (computerChoice === "paper") {// Inner to Outer
    return "the Computer Won!";
  } else { // + 2.4
    return "You Won!!!";
 };// +2.4   
 
 if(userChoice === "paper") { //3.1 Outer
   if(computerChoice === "scissors"){
     return "the Computer Won!"
   } else {
     return "You Won!!!"
   }  

 }; // 3.1 Outer
  };// +2.1 Outer
};// +1.1 Outer 

console.log(determineWinner(`paper`,`scissors`));

That is exactly what I mean. This allow the people helping to run the code in the lesson and look at the instructions. We don’t know all the exercises from the top of our head, there are simple to many exercises

first, closing curly brackets of if should not have a semi-colon:

 if(userChoice === computerChoice) { // Inner to Outer
return "the Game was a tie!"
 }; // semi-colon shouldn't be here

if you want a semi-colon, place it a line earlier (after returning the string)

despite your massive amount of comments to get the condition nesting right, you got it wrong I am afraid

your 3.1 (to check if userChoice is paper) is nested within if userChoice is rock

so when userChoice is paper, this line:

if (userChoice === "rock") { // +2.1

will be false, so the code goes here:

  };// +2.1 Outer

the check userChoice is paper should be outside/after checking if userChoice is rock

2 Likes

I tried to make UserChoice Paper Go inside of UserChoice Rock; like you said. as you can see I move the Bracket from line 42 to 51 to be more inclusive but i still got UNDEFINED… I really can’t understand, you to link-up the statements…

const determineWinner = (userChoice,computerChoice) => {// + 1.1 Outer
 if(userChoice === computerChoice) { // Inner to Outer
return "the Game was a tie!";
 }// 1.2 and Inner to Outer. + Semi Colon to End of String.
// if the user chose ( ) but if the user didn't choose Rock then the other possible outcomes are xxxx...
if (userChoice === "rock") { // +2.1
  if (computerChoice === "paper") {// Inner to Outer
    return "the Computer Won!";
  } else { // + 2.4
    return "You Won!!!";
 // +2.4   
 //Step 9
 if(userChoice === "paper") { //3.1 Outer
   if(computerChoice === "scissors"){
     return "the Computer Won!";
   } else {
     return "You Won!!!";
   }
     
 };
 }; // 3.1 Outer
  };// +2.1 Outer
};// +1.1 Outer 

console.log(determineWinner(`paper`,`scissors`));

Now you made it worse? What we want to is the following:

if(userChoice === computerChoice) {

}
if (userChoice === "rock") {
   // determine if user or computer won
} 
if (userChoice === "paper") {
   // determine if user or computer won
} 

nesting won’t work here. We check the different user choices, and within each possible user choice we determine if user or computer won

I would recommend to step through your code here:

http://www.pythontutor.com/visualize.html#mode=edit

to understand the flow of your program better.

There is only three possible places the closing curly brackets can go. After the First If Statement and Block, After the Second Statement and Block or After the Console Log. I’ve tried everthing you said and its still not working??

Also Have a Question in this Text?
Question

you shouldn’t look at curly brackets at: I should place this curly bracket there. You should understand what this does to the flow of your program

The amount of curly brackets depends on what you attempt to close and how many nesting you use in conditions.

I grasp, that where you put the closing bracket, effect where the if condition and its branches are applied… but, if you think about it… you can’t control the amount of curly brackets that are produced because each if condition has a pair of curly brackets… or have i missed the point?

but if you one if condition:

if (condition) {

}

you one set of curly brackets. While if you have a nested if condition:

if (condition1) {
   if (condition2) {

  }
}

you have two set of curly brackets, one for each condition/if.

I am with you so far. I put my closing brackets to follow exactly the same positions as yours … but error

const determineWinner = (userChoice,computerChoice) => {// + 1.1 Outer
 if(userChoice === computerChoice) { // Inner to Outer
return "the Game was a tie!";
 }// Why does this bracket, closing this Block when the other closing brackets go after two blocks?
 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!";
    
    }
}

console.log(determineWinner("rock","scissors"));

you don’t close your function anywhere:

const determineWinner = (userChoice,computerChoice) => {// + 1.1 Outer

no } to match the opening curly bracket of the function

1 Like