9. Next steps /build "rock, paper, scissors"


#1

Hi,

I know that my code is not ready yet, but I wonder what is wrong with it so far. It says on the black box: "SyntaxError: expected expression, got keyword 'else'"

var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else if (computerchoice <= 0.87) {
    computerChoice = "scissors";}
     else {computerchoice = "rope";}
 console.log("Computer: " + computerChoice);
var compare = function (choice1, choice2){
if (choice1 === choice2) {return ("The result is a tie! Make a new choice!");}

// What if computer chooses rock?
else if (choice1 === "rock") {if (choice2 === "scissors") {return ("rock wins");} 
{if (choice2 === "rope") {return ("rope wins");}
{if (choice2 === "paper") {return ("paper wins");}
}

// What if computer chooses paper?
else if (choice1 === "paper") {if (choice2 === "rock") {return ("paper wins");}
{if (choice2 === "scissors") {return ("scissors wins");}
{if (choice2 === "rope") {return ("paper wins");}

//what if computer chooses scissors?
else if (choice1 === "scissors") {if (choice2 === "paper") {return ("scissors wins");}
{if (choice2 === "rock") {return ("rock wins");}
{if (choice2 === "rope"] {return ("scissors wins");}

//What if computer chooses rope?
else if (choice1 === "rope") {if (choice2 === "rock" {return ("rope wins");}
{if (choice2 === "paper") {return ("paper wins");}
{if (choice2 === "scissors") {return ("scissors wins");}

};
var userChoice = prompt ("rock, paper, scissors, rope?");
compare(userChoice, computerChoice);

Has it something to do with this maby?:

var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else if (computerchoice <= 0.87) {
    computerChoice = "scissors";}
     else {computerchoice = "rope";}

Edit: it also says: "Oops, try again. There was a problem with your syntax."


#2

Throwing your code in http://jsbeautifier.org/ it seems like your nesting your else if statements where each case for choice1 should be on the same level.


#3

It still gives the same answer. About that jsbautifier.org, does it somehow show what is wrong with the code? I don't really understand that site.

var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else if (computerChoice <= 0.87) {
    computerChoice = "scissors";}
     else {computerChoice = "rope";}
 console.log("Computer: " + computerChoice);
var compare = function (choice1, choice2){
if (choice1 === choice2) {return ("The result is a tie! Make a new choice!");}

// What if computer chooses rock?
else if (choice1 === "rock") {if (choice2 === "scissors") {return ("rock wins");}  {if (choice2 === "rope") {return ("rope wins");} {if (choice2 === "paper") {return ("paper wins");}
}

// What if computer chooses paper?
else if (choice1 === "paper") {if (choice2 === "rock") {return ("paper wins");} {if (choice2 === "scissors") {return ("scissors wins");} {if (choice2 === "rope") {return ("paper wins");}

//what if computer chooses scissors?
else if (choice1 === "scissors") {if (choice2 === "paper") {return ("scissors wins");} {if (choice2 === "rock") {return ("rock wins");} {if (choice2 === "rope"] {return ("scissors wins");}

//What if computer chooses rope?
else if (choice1 === "rope") {if (choice2 === "rock" {return ("rope wins");} {if (choice2 === "paper") {return ("paper wins");} {if (choice2 === "scissors") {return ("scissors wins");}

};
var userChoice = prompt ("rock, paper, scissors, rope?");
compare(userChoice, computerChoice);

#4

It just formats your code in a nicer way, no error checking and not solution. But as you see that your code has clearly a diagonal shape after using this formatting tool you can see that you nest your else ifs inside each other where the structure should rather be something like this:

if(choice1 === ...){ sub cases}
else if(choice1 === ...){ sub cases}
else if(choice1 === ...){ sub cases}
else if(choice1 === ...){ sub cases}
else if(choice1 === ...){ sub cases}

#5

Is this the right way to start? I ahve been stuck with this for more than 3 weeks. Is the idea right?

// What if computer chooses rock?
else if (choice1 === "rock") {
    if (choice2 === "scissors") {
        return ("rock wins");
    } {
        if (choice2 === "rope") {
            return ("rope wins");
        } {
            if (choice2 === "paper") {
                return ("paper wins");
            }
        }
    }
}

#6

Why do you have so many {}?

// What if computer chooses rock?
else if (choice1 === "rock") {
    if (choice2 === "scissors") {
        return ("rock wins");
    } else if (choice2 === "rope") {
        return ("rope wins");
    } else if (choice2 === "paper") {
        return ("paper wins");
    }
}

#7

Ok you have unnecessary opening brackets... i will point out the ones...

// What if computer chooses rock?
else if (choice1 === "rock") {
    if (choice2 === "scissors") {
        return ("rock wins");
    } { <<-- Unnecessary..
        if (choice2 === "rope") {
            return ("rope wins");
        } {  <<--Unnecessary...
            if (choice2 === "paper") {
                return ("paper wins");
            }
        }<<-- Unnecessary
    } <<-- Unnecessary
}

#8

var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else if (computerChoice <= 0.87) {
    computerChoice = "scissors";}
     else {computerChoice = "rope";}
 console.log("Computer: " + computerChoice);
var compare = function (choice1, choice2){
if (choice1 === choice2) {return ("The result is a tie! Make a new choice!");}

// What if computer chooses rock?
else if (choice1 === "rock") 
{if (choice2 === "scissors") {return ("rock wins");}  if (choice2 === "rope") {return ("rope wins");} if (choice2 === "paper") {return ("paper wins");}}


// What if computer chooses paper?
else if (choice1 === "paper") 
{if (choice2 === "rock") {return ("paper wins");} if (choice2 === "scissors") {return ("scissors wins");} if (choice2 === "rope") {return ("paper wins");}}

//what if computer chooses scissors?
else if (choice1 === "scissors") 
{if (choice2 === "paper") {return ("scissors wins");} if (choice2 === "rock") {return ("rock wins");} if (choice2 === "rope") {return ("scissors wins");}}

//What if computer chooses rope?
else if (choice1 === "rope") 
{if (choice2 === "rock" {return ("rope wins");} if (choice2 === "paper") {return ("paper wins");} if (choice2 === "scissors") {return ("scissors wins");}}


var userChoice = prompt ("rock, paper, scissors, rope?");
compare(userChoice, computerChoice);

I tried to take those extra brackets out but it still says: "Oops. There was some problem wiht your syntax. SyntaxError: missing ) after condition"


#9

I was not sure how many I need them. Like if there is a need to oput a bracket for example in front of this. In front of "if choice2 ===...":

if (choice2 === "scissors") {return ("rock wins");}

#10

at least here you missng the ) to close the condition and your seem to lack 2*} to close the last else if and the function.
You can also format your code by:
http://jsbeautifier.org/
to see which {} aren't matched yet.


#11

These should be right now, but the text says they are not?

//what if computer chooses scissors?
else if (choice1 === "scissors") 
{if (choice2 === "paper") {return ("scissors wins");} if (choice2 === "rock") {return ("rock wins");} if (choice2 === "rope") {return ("scissors wins");}}

//What if computer chooses rope?
else if (choice1 === "rope") 
{if (choice2 === "rock") {return ("rope wins");} if (choice2 === "paper") {return ("paper wins");} if (choice2 === "scissors") {return ("scissors wins");}}

So that jsbeautifier does not correct anything. it just puts the code in a bit different way so that it is easier to correct? Nests in a different way?


#12

Yes. You can also run it with labs repl.it which tells you the line where something went wrong.


#13

Thank you so much for the lab link. It is extremely helpful. One “stupid” question: why does this last line have to be corrected like this? Does every code has to end with this bracket } and semicolon, no matter what language?

compare(userChoice, computerChoice);};

```

#14

No as said earlier you missed to }} to close the last else if and the function and as this statement calls the function you might have some trouble if this functions isn't even written completely (still not closed }).


#15

I added also what happens in the player makes an inappropriate choice. This is probably ok way to write it?

var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else if (computerChoice <= 0.87) {
    computerChoice = "scissors";}
     else {computerChoice = "rope";}
 console.log("Computer: " + computerChoice);
var compare = function (choice1, choice2){
if (choice1 === choice2) {return ("The result is a tie! Make a new choice!");}



// What if computer chooses rock?
else if (choice1 === "rock") 
{if (choice2 === "scissors") {return ("rock wins");}  if (choice2 === "rope") {return ("rope wins");} if (choice2 === "paper") {return ("paper wins");} else {console.log ("Invalid choice!");}}


// What if computer chooses paper?
else if (choice1 === "paper") 
{if (choice2 === "rock") {return ("paper wins");} if (choice2 === "scissors") {return ("scissors wins");} if (choice2 === "rope") {return ("paper wins");} else {console.log ("Invalid choice!");}}

//what if computer chooses scissors?
else if (choice1 === "scissors") 
{if (choice2 === "paper") {return ("scissors wins");} if (choice2 === "rock") {return ("rock wins");} if (choice2 === "rope") {return ("scissors wins");} else {console.log ("Invalid choice!");}}

//What if computer chooses rope?
else if (choice1 === "rope") 
{if (choice2 === "rock") {return ("rope wins");} if (choice2 === "paper") {return ("paper wins");} if (choice2 === "scissors") {return ("scissors wins");} else {console.log ("Invalid choice!");}}


var userChoice = prompt ("rock, paper, scissors, rope?");
compare(userChoice, computerChoice);};

I mean this:

else {console.log ("Invalid choice!");}


#16

Two things first better use this outside of the function otherwise your code will not run.
And the else thing is ok but it would probably be either if you add an else to your row of else ifs as it is more likely that the user will make an invalid input than that the computer will make one.


#17

Outside of the function?


#18

Yes

}; //end of the function 
var userChoice = prompt ("rock, paper, scissors, rope?");
compare(userChoice, computerChoice);

#19

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

var compare = function (choice1, choice2){
if (choice1 === choice2){
return "The result is a tie!";
}

    // What if choice 1 is rock?

else if (choice1 === "rock") {
    if (choice2 === "scissors"){
        return ("rock wins");
        }  
    if (choice2 === "rope"){
        return ("rope wins");
        } 
    if (choice2 === "paper"){
        return ("paper wins");
        } 
    else {
        console.log ("Invalid choice!");
    }
}

// What if choice1 is scissors?

else if (choice1 === "scissors"){
    if (choice2 ==="paper"){
        return "scissors win";
    }
    if (choice2 === "rock"){
        return "rock wins";
    }
    if(choice2 === "rope"){
        return "scissors win";
    }
    else{
        console.log("Invalid choice!");
    }
}

}

hi... I am having trouble here despite having the correct output i still get the message Oops, try again. Make sure to call your compare function with userChoice and computerChoice as the arguments!

I have no clue what to do here as I already have my userChoice declared. plzzzzz help


#20

Now it gets totally wrong.

var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else if (computerChoice <= 0.87) {
    computerChoice = "scissors";}
     else {computerChoice = "rope";}
 console.log("Computer: " + computerChoice);
var compare = function (choice1, choice2){
if (choice1 === choice2) {return ("The result is a tie! Make a new choice!");}



// What if computer chooses rock?
else if (choice1 === "rock") 
{if (choice2 === "scissors") {return ("rock wins");}  if (choice2 === "rope") {return ("rope wins");} if (choice2 === "paper") {return ("paper wins");}}
else {console.log ("Invalid choice!");}


// What if computer chooses paper?
else if (choice1 === "paper") 
{if (choice2 === "rock") {return ("paper wins");} if (choice2 === "scissors") {return ("scissors wins");} if (choice2 === "rope") {return ("paper wins");}} 
else {console.log ("Invalid choice!");}

//what if computer chooses scissors?
else if (choice1 === "scissors") 
{if (choice2 === "paper") {return ("scissors wins");} if (choice2 === "rock") {return ("rock wins");} if (choice2 === "rope") {return ("scissors wins");}}
else {console.log ("Invalid choice!");}

//What if computer chooses rope?
else if (choice1 === "rope") 
{if (choice2 === "rock") {return ("rope wins");} if (choice2 === "paper") {return ("paper wins");} if (choice2 === "scissors") {return ("scissors wins");}}
 else {console.log ("Invalid choice!");}


var userChoice = prompt ("rock, paper, scissors, rope?");
compare(userChoice, computerChoice);};