6/9 "Syntax error: Unexpected end of input"


#1

So I'm not sure what I'm doing wrong; I've looked over it a bunch of times already :confused: . I keep getting the syntax error: unexpected end of input. Can somebody please help me out here.
Here is my 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";
}

#2

This normally indicates that something is still open and therefore the end of the program comes unexpected. Here I'd guess that it is a missing } at the end to close the function.


#3

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

You are thinking of something like this I would guess.


#4

The problem with this is that the else would include far too many cases that is why you nest the choice2 part and the else in the else if rather than doing this. (hint of 6/9)


#5

I see that this program like this ain't gonna do much but I only wrote what I see that makes no sence in his code. Even my code isn't gonna do anything special.
He seems a little bit lost with this asignment that is for sure and he won't end it on his own unless he starts to understand the fundamentals of :

  1. table of equality
  2. Conditions (and, or, not)
  3. if/else statements and functions and many more....

#6

Yes conditions are essential for this exercise but and,or and not are not really required here (nothing wrong with using them if you know them already but afaik they haven't been introduced at this point). So nesting condition should be easier here. Also your code works for this particular exercise but it won't for the next.

@pysurfer97415 The general idea in the else if cases is that you already dealt with the tie case and now you only look at what choice1 can become which leads you to 3 cases:

choice1 === rock
choice1 === paper
choice1 === scissors

and for each of this cases you have 2 options left for choice2 (1 is already dealt with by the tie case). So the else if is about the choice1 and inside you nest an if/else that handles the options for choice2.


#7

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

This worked for me and I think it's the problems of open braces.