7. what if choice1=paper / Syntax error : got keyword : "else"


#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("Computer: " + computerChoice);*/


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

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

#2

so we created are compare function, and an if statement to check if the result is a tie.

Then we create an else if statement to check for user choice1:

else if(choice1 === "rock") {

}

inside your we create an if and a else to check for choice2:

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

so this is one of the three options for choice1, then we add a next else if:

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

i added a comment, not going to code everything. But see how i add this else if after the } which closes the else if choice is rock?

Now look at your code, and follow this structure. (recommendation: good indention so you can keep track of all the {})

If you need more help, post an updated version of your code


#3

Thank you for your reply, appreciated, here is my full code....

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!"
}
} 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 if(choice1 === "scissors") {
   if(choice2 === "paper") {
    return"scissors wins";
} else {
    return"rock wins";
};

#4

but you already progressed to 8? I meant if you need more help with 7. what if choice1 is paper


#5

sorry i successfully passed 7, it was error of who wins, and answer to ur querry what if choice1 is paper, then paper wins if not scissors wins..


#6

Sorry, what?

here:

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

this is currently your compare function, shouldn't all the else if statements be part of your compare function as well?


#7

yes it should be as we are comparing things but how we add to the else if in other 3 conditions...


#8

you can just follow the structure i gave you:

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

this is all part of the compare function


#9

ok its about how to put proper tags {, i followed your structure but still i get syntax error, in the end i simply use } or };
/*here is the code plz take a look and thank u 4 helping me*/
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(userChoice, computerChoice) {
if(userChoice === computerChoice) {
return"The result is a tie!"
}
}
else if(userChoice === "rock") {
if(computerChoice === "scissors") {
return"rock wins";
} else {
return"paper wins";
}
}
else if(userChoice === "paper") {
if(computerChoice === "rock") {
return"paper wins"
} else {
return"scissors wins";
}
}
else if(userChoice === "scissors") {
if(computerChoice === "paper") {
return"scissors wins"
} else {
return"rock wins"
}
}


#10

this is currently your function:

var compare = function(userChoice, computerChoice) {
if(userChoice === computerChoice) {
return"The result is a tie!"
}
} // closes function

you should place all the else if inside the function


#11

it is successfully done, now i got it, sorry and thanks a lot for ur help...thank u.


#12