8.What if choice1 is scissors?


#1

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

    }




     if (choice1 === "paper")
        {
            else if(choice2 === "rock")
                {
                    return "paper win";
                }

            else 
                {
                    return "scissors wins";
                }

              if (choice1 === "scissors")
                {
                else if(choice2 === "paper")
                {
                    return "scissors win";
                }

                else 
                {
                    return "scissors wins";
                }
        }    



}

};

It states I have a sytnax error, I cant find a syntax error, can some please help me?


#2

With syntax error it means you have left out some brackets or colons or put one too many. I looked through your code and i made little changes, sorry if you wanted to figure this out on your own. What would be good next time is:

  1. Indentation is your friend.
  2. Keep track on your opening and closing brackets.

Here is your program, without empty spaces and corrected brackets ( i think the previous code had one bracket too soon and therefore broke the whole if,else if, else statement block ) :relaxed:

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

#3

thank you , the code is a lot neater and easier to read :smiley: but when i try and run it , it throws an error :" Make sure to call your compare function with userChoice and computerChoice as the arguments! " . Now how would I do that? :sweat:


#4

You are not calling out the function you made "compare". Do this in the very end after

};

Hint: use console.log and compare(arguments)