Build Rock, Paper, Scissors Lesson 8 JS


#1

I can't find my mistake!

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);
choice1 = userChoice;
choice2 = computerChoice;

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

the computer says there is a problem with the syntax.


#2

Working on this now too....if I come up with something, I will let you know :slightly_smiling:


#3

Ok, got it finally! Check out below:

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) {
compare(userchoice, computerChoice);

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


#4

**Arguments are passed to functions like this:
compare(userChoice, computerChoice);

Just change that out from your:
choice1 = userchoice;
choice2 = computerChoice;

and I think you should be good to go!


#5

Here you use a semicolon after a condition:
else if (choice1 === "paper");
else if (choice1 === "scissors");

It would also help a lot for you and others if you format your code nicely:

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

PS: The color is done by using three backticks ` at the beginning and end of your code (and blank lines separating from text blocks)