7/9 Need help, unexpected token else


#1

Hi I keep getting Syntax Error: unexpected token else, how do I solve this?
Thank you :


/*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 wins";
        }
    } else {
        return "scissors wins";
    }
    }
    };


#2

the structure you need to achieve:

else if (condition){
  if (condition){
    // choice1 wins
  } else {
    // choice2 wins
  }
}

the if and else should be inside your else if, but you already close the else if before you start your else


#3

I have this same error message, and have not found any consistent answer yet in this forum as to what the problem is. I can't seem to see the error or what is exactly missing.
Need help with this section.
Here is my code:

//LSN #8 - WHAT IF CHOICE 1 IS SCISSORS
else if(choice1 === "scissors"){
if(choice2 === "rock"){
return "rock wins";}
}
else{
if(choice2 === "paper"){
return "scissors wins";}
}
}
compare(userChoice, computerChoice);

Thanks in advance


#4

can i see your full code?


#5

Thank you for responding back, I finally got it fixed. However, I am having a different problem now. I am working on Unit 3 Search Your Name, in lesson 3 the page stopped responding and I could not submit my for loop code. So I have moved on to next lesson, Your if statement, can't seem to get the code to work here either. I typed in the code from lesson 3 (i.e. for loop) and added the if statement but no go.
Not really finding any info in the Q & A Forum about this lesson either. Do you have any info on this? Also would like to know is there any way to search the problems in these lessons for others' questions and solutions without going thru forum. I am very new with coding and I have spent the last two days going through the forum for almost every lesson in JS. It's been very time consuming and I have to complete all the tutorials by Wed, (6/22) and test on Thurs (/23), so I need to be able to find the code examples quickly and move on. I do not have any one to help me at home and I have never coded in any language before. Just completed HTML & CSS last week all brand new to me. Sorry for the long post, just need to get moving with all this. Thank you in advanced if you can take a look at my code and let me know if I have something wrong( duh obviously :slight_smile:

Ok here's my code: this is lesson #4 Your if statement-

var text= " Hey Cheryl is terrifc\ she is learning to code. Cheryl is volunteering for the summer.";
var myName= " Cheryl";
var hits= [];

for(var i =0; 1if (text[i] === "C")


#6

please read the guidelines, please create a new topic, this a new question