What if choice1 is paper?


#1


Hi there. So when trying to run the function it says "SyntaxError: Unexpected keyword 'else'". I used the exact same format as i did for What if choice1 is rock and that function didn't have a problem.

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

else {
        return "paper wins";
    }
} 
};  //remove this bracket.

#3

That unfortunatly did not work.


#4

i just said the error.you have to call the function after it.give me your updated full code.


#5

It may be the semicolon after the bracket that is still on that line before the next else if.


#6

yeah ! may be he just removed } .but he should remove that too


#7

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

now you have to call the function.without calling it'll not work

how to call a function you can check it here


#9

Thank you guys for helping. I got it all sorted out now.


#10

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