What is wrong with my code?


#1

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

Make sure you are closing your if/else is/else statements (and nested ones) properly.
You are missing a } after your nested else statements:

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

#3

var compare = function (choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    }   
   else if (choice1 === "rock") {
    if (choice2 === "scissors") {
        return "rock wins";
    } //Here you need another } to close your else statement. The one you already have is closing your if statement.
    else { 
        return "paper wins";
    }
    else if (choice1 === "paper") {
    if (choice2 === "rock") {
        return "paper wins";
    }
    else { 
        return "scissors wins";
    }
}

You're missing a } between your first else if and your first else statements. You need to close the else if.


#4

one minor fix ... been on it for week , thank you


#5

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