Sigh.... Cant seem to get the right code. HELP please


#1


Hi all, Didnt want to ask, but I have been at this for a while trying to figure it out..... I am clearly missing something very simple. Please clarify for me where I went wrong.
Thanks

Problem is " SyntaxError: Unexpected token else"


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

compare(userChoice, computerChoice);


#2

closing curly brackets (}) of if, else if and else shouldn't have semi-colons, it interrupts the if/else if/else clauses from working properly.


#3

OMG thank you! Amazing how the simplest things can be overlooked


#4

welcome to the wold of programming, where a simple mistake can a whole lot of error messages, good you solved the issue


#5

SOOOO, I wanted to play around a little.
Say I wanted to print on the console the winner of the game via "("user/computer is the winner")

I am writing the code that would give the result of the winner from the compare function that we are asked to write before.

Here is my code:

var result= function (result1){
    
    if (result1 === str.inlcudes ("tie");) {
        return "Nobody wins";
    }
        
    else if (result1 === str.inlcudes ("wins");) {
        return: "User is the winner";
    }    else {
            return: "Computer is the winner";
        }  
} 

result(choice1);

What I am basically going for is, if I identify a key word in the choice function[str.includes()], that has a result of either a 'tie or wins', then the function will determine which key word it was assigned to [user/computer] and spit out the winner.

I am messing around to see if this would work. When i run it, I get "SyntaxError: Unexpected token"
not sure if its just a small error or if the code overall is completely flawed. lol.

Any corrections or wisdom into how I can make this code work. Thanks in advance for the souls trying to solve this with me :slight_smile:

P.S sorry if I am not clear i my explanations. Hopefully the code will illustrate my logic.

Cheers


#6

your code is quit flaws, here is the improved version:

choice1="tie";
var result= function(result1){
    
    if (result1.includes("tie")) {
        return "Nobody wins";
    }
        
    else if (result1.includes("wins")) {
        return "User is the winner";
    }   
    else {
        return "Computer is the winner";
    }  
};

console.log(result(choice1));

changes i made:
- removing semi-colons in condition, semi-colons don't seem to be your strong suit, maybe read about when to use them and when not?
- str.includes is wrong, str is short for string, so str should be the string that should hold the include, so in your case result1.


#7

Ahhh I see what you did. Thank you for taking the time to explain that to me. I agree, I need to be careful with them semi-colons!

Really appreciate your insight stetim94 :slight_smile:


#9

3 posts were split to a new topic: Choice1 is rock


#10

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