What is stopping this code to execute


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/1/5?curriculum_id=506324b3a7dffd00020bf661


SyntaxError: Unexpected token else

Kindly correct me . have I missed something

Also where to put console.log to print who won after choices are made by user and PC


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"}
        }
       compare (userchoice ,computerchoice)
        

    else if (choice1 === "paper"){
        
        if (choice2=== "rock"){
            return "paper wins"}
            
        else {
        return "scissors wins"}
        }  

     compare (userchoice ,computerchoice)
        
    else if (choice1 === "scissors"){
            
        if ( choice2 === "paper"){
                return "Scissors wins"}
                
        else {
        return "rock wins"}   
        }   
        compare (userchoice ,computerchoice)
} ;


#2

Hi Yugantika, a few points:

  • You're missing semicolons from a few lines (all of the return lines need a semicolon at the end). That's the reason the else is unexpected, because it's expecting a semicolon.

  • You need to be careful how you format your code in terms of curly brackets and indentation, which may be the reason you've not noticed the missing semicolons.

  • You've misunderstood when to use the compare function you've created. It should only be used after you've declared the whole function (i.e. at the end of the code).

This is what you should be aiming for:

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

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);

Notice that proper use of the curly brackets and indentation makes code far easier to read, and therefore less likely to produce (or miss) mistakes.


#3

Mr.nedwards

As I am new to this coding stuff I often get confused on using these functions and loops.
I appreciate you for clearing my doubts in very precise manner.
Your advice on using function was helpful.

My code is now executing.!

Regards
Yugantika


#4

No problem!

If you're having trouble with things like indentation, I would also recommend having a go at learning Python at some point. Python doesn't use curly brackets, but it DOES force you to indent code properly (in other words, the code fails if it isn't indented correctly), so it's a very good way to practice.

The following link also may give you some ideas on how you want to format your code in the future:

Good luck :slightly_smiling: