What am I missing? (Lesson 4, part 7)


#1



Lesson 4, part 7.


Tells me this: "Oops, try again. Your code returned 'paper wins' instead of 'scissors wins' when the inputs are paper and scissors" I think there's an else if statement wrong somewhere, but I have rewrote it a few times.

But no luck.


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


#2

As you can see on your last two if statements, they are nested. One of them has an else statement, but the other one doesn't.


#3

@apoc1216

I think the problem comes from the else if nesting structure.

else if (choice1 === "rock") {
   //we test choice 2
   if (condition) {
     //something happen
   }else {
     //something happen
   }
}
else if (choice1 === "scissors") {
  //same thing here
}

#4

Add an else statement for your first if statement then nest {} both accordingly. :slight_smile:


#5

I tried that but it told me "unexpected token else" I even accounted the {} accordingly but nothing :frowning:


#6

Post your corrected code and let's see what else to tweak abit :grin:


#7

Sorry, here ya go:

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";
}
}

}


#8

Add one more } after this line so there should be two under it.


#9

And the last bracket should have a semi colon after it. ;


#10

Nope :frowning: still shows the same else token unexpected error. Here's the code:

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";
}
}

};


#11
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";
    }
}
};

Try indenting your code like this..

You had an extra bracket here,'

And here,


#12

Okay I see what I did now. That's frustrating, but I know what happened. Thanks!


#13

Your welcome! If that was the answer you were looking for feel free to click the solution button below my reply. :grin: