What if choice1 was paper?


#1

A lof of people seem to have issues here but even after reading other threads, I can't identify what's wrong with my code prompting the same error of "Unexpected token else". It doesn't seem to be excess semicolons or unclosed braces/brackets. Any suggestions?

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

#3

I was just looking through it and I'm not sure that was correct either. Your code also seemed to have two choice2's where rock would win. Looking over my code, I think I've closed off all the braces accurately already @first_class_html - am I missing something?


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

The second case seems to be ok, but the code above is pretty mean as you only have 3 options:
- tie
- rocks wins in case your opponent has chosen scissors
- paper wins in any other case


#5

Ah yes, I figured that out last night thanks @haxor789. What's interesting though, and I'm not sure you're at all associated with Codeacademy yourself, but the above piece was transferred directly from the previous lesson as 'approved' in order for me to pass to lesson 7 so as a student, I had assumed that there shouldn't be any error in that code. Thanks anyway!


#6

The problem is that there are a few options to do every exercise. So the test function often considers your code to be a blackbox and only analysis if a certain input creates an expected output. And for this specific exercise your code actually was sufficient. Ex.6 only checks for choice1 being rock so the possible outcomes are tie, rock wins in case of scissors and paper on else. For later exercises however it is not sufficient.


#7

Gotcha. Thanks for the explanation and all your help so far @haxor789!