#8 What if Choice 1 was scissors?


#1


https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/4?curriculum_id=506324b3a7dffd00020bf661#


my code starts the program but can't finish.
Do I have too many open or closed brackets somewhere? Error given is: "Oops, try again. Your code returned 'scissors wins' instead of 'undefined' when the inputs are scissors and rock"
Any help is appreciated


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 "rock wins";
    }
    else {
        return "scissors wins"
    }
}
}
console.log(compare(userChoice,computerChoice));


#2

Remove the console.log() so it should just be compare(userChoice,computerChoice);

Put a semi colon after the last closing bracket of the function,

should be,

}
}
};


#3

Thank you for the quick reply! I changed those two issues and re-ran and got the same error. Below is my "new" 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 "rock wins";
}
else {
return "scissors wins"
}
}
};
compare(userChoice,computerChoice);


#4

Replace the last else if by a simple else...

if(/*condition*/){
    //instructions
}else if(/*condition*/){
    //instructions
}else{
    //instructions
}

#5

Thank you for the reply.... I am now a little confused. I thought the "proper" form was
else if(condition){
instructions}
if(condition){
instructions}
else{instructions}

that is the format I have been using for this lesson and have passed so far. How does it switch to
if(condition){
instruction
} else if(condition){
instructions
} else { instructions} ?????

if I take out the "else" as suggested I get "Syntax error"

Oops, try again. There was a problem with your syntax. SyntaxError: Unexpected token '{'. Parse error.
Here is my code with the "if" taken out

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


#6

This is taken from the first code you presented. Check my comments.

Below is the problem in your newest code:

You still haven't resolved the outcomes relating to choice1==="scissors".


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.