What's wrong with this code? It's exactly the same as someone else's code that ran just perfectly!


#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
  1. I would never recommend you take some one elses code to do the work for you... (not saying you did so but still) It doesn't work like that and honestly it doesn't do you good in the future.
  2. I'm pretty sure this code wouldn't get you anywhere past lesson one. It isn't declaring UserChoice so it can't move on. I'm kinda hoping this isn't the full code because if it is I'm not sure how far you got.
  3. I would suggest restarting and trying to do it over because the code doesn't look like it'd solve much past 1

#3

I didn't copy anyone else's code, just used it to compare to mine, corrected my errors, but it still shows SyntaxError: Unexpected token ILLEGAL.
This isn't the full code, just a part of it.
Ok, I'll try


#4

It would've been better if you had posted the full code because it changes how we can help you.
I will apologize if I made it sound like you copied it wasn't my intention.
Good luck with your new code!


#5

It's alright.
Here's my full 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 if (choice1 === "scissors"){
    if (choice2 === "rock"){
        return "rock wins";
    }
    else {
        return "scissors wins";
    }
}

compare(userChoice, computerChoice);

I still haven't quite got the hang of if/else statements, I guess.


#6

Yeah those are hard to remember. Hopefully the code works out in the end.

I'd go back to some later lesson to review that stuff...

The error the code will continue having is Unexpected Token Else
You need to fix else and start from there with fixing.


#7

I would compare this to your other nested statements and see if something looks different