8. What if choice1 is scissors?


#1

Can someone tell me what is wrong with my 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 (choice1 === "scissors") {
if (choice2 === "rock") { return "rock wins"; }
else (choice 2 === "scissors") { return "scissors wins"; } }

compare(userChoice,computerChoice)


#2

var compare = function(choice1,choice2); <---

The function shouldn't have a semi-colon " ; ", you need to have an open bracket instead. Also the opening and closing brackets are extremely hard to read. It's easiest if the closing brackets are on their own line.


#3

It still says SyntaxError: Unexpected token "{" but i don't know where the bug is!

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 === "rock") { return "rock wins"; }
else (choice 2 === "scissors") { return "scissors wins"; }
}
};

compare(userChoice,computerChoice)


#4

This should help!


#5

Hi @sinsabrina I think i found your issues :smile:
below is your code with my notes at the bottom:

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"; <-- note 1
        }
    } 
    else (choice1 === "scissors") { <-- issue 1 
        if (choice2 === "rock") { 
            return "rock wins"; 
        }
        else (choice 2 === "scissors") {   <-- issue 2
            return "scissors wins"; 
        } 
    }
};

issue 1: for this bit of code I still used else if since the instructions said to do it the exact same as the previous 2 sections.
issue 2: this is the bigger problem here. the bit in the parenthesis is an error. To fix the code you should remove the condition to make the line read else { return "scissors wins"; } (it should mirror the line i pointed out as "note 1")
by including the condition of else (choice 2 === "scissors") you're saying if scissors === scissors, then scissors wins, which is probably not what you want to do.
also, there is a space between "choice" and "2".