8/9 syntaxerror: missing before statement


#1

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(compare(userChoice,computerChoice)){
var choice1=userChoice
var choice2=computerChoice
}
var compare=function(choice1, choice2){
if(choice1===choice2){
return "The result is a tie!";
}
else if(choice1==="rock"){

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

}


#2

on these lines

var userChoice=prompt("Do you choose rock, paper, or scissors?"){
var computerChoice=Math.random()}

you have to get rid of the {} because we are just creating variables and not functions

var userChoice=prompt("Do you choose rock, paper, or scissors?");
var computerChoice=Math.random();

Now you have to get rid of this lines because there is a syntax error

console.log(compare(userChoice,computerChoice)){
      var choice1=userChoice
      var choice2=computerChoice
}

The code above will return an error because from the way the compare function was defined it will only be processed at run time so using it before its declaration will cause an undefined error
You can call just this at the end of your code

console.log(compare(userChoice,computerChoice))

#4

something like this

var userChoice = prompt("Do you choose rock, paper or scissors?").toLowerCase();
var computerChoice = Math.random();
var tie =  true;

if (computerChoice < 0.34)      {computerChoice = "rock";} 
else if(computerChoice <= 0.67) {computerChoice = "paper";} 
else                            {computerChoice = "scissors";} 

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

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

console.log(compare(userChoice, computerChoice));

#6

These changes still bring up an syntax error with the if statements after I followed your example. Is it because of where I'm placing the brackets for the functions? I seem to have a lot of trouble with the syntax error for if and else statements. Can you please explain to me how to minimize these errors? Thanks so much for your help and your valuable time.

var userChoice=prompt("Do you choose rock, paper,or scissors?");
var computerChoice=Math.random();
var tie=true;
if(computerChoice<0.34){
computerChoice="rock";}
else if(computerChoice<=0.67){
computerChoice="paper";}
else {
computerChoice="scissors";}
console.log(compare(userChoice,computerChoice))
var compare=function(choice1, choice2)
if(choice1===choice2){
return "The result is a tie!";}
else if(choice1==="rock"){

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

}


#7

You forgot to open your compare function with a brace

var compare=function(choice1, choice2){

#9

Have you been able to pass?


#10

yes, I've been able to pass.


#11

Hey, I have the same problem with syntax. Can you help me too with checking my code? I tried without bold text and I pased, but I thought I need to write it because of instructions.

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";}

var compare = function (choice1,choice2) {

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

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

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

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

}
console.log(compare(userChoice,computerChoice))


#12

see on this line you have

else (choice1 ==="scissors"){

it should just be

else {

the else block takes no conditions.


#13

Thanks :smile: It is good to know about else block.