简体   繁体   中英

Codecademy javascript lesson 7/9 error

I've been stuck on 7/9 for 2 nights now. This is a rock, paper scissor game. I can't figure out what is wrong. I tried the online lint and it also says my line 22 is an error(Expected an identifier and instead saw 'else'). Following the instructions I wrote another else if under the existing code inside the compare function.

my code :

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

you're terminating you're else if after the condition with ;

it should be:

else if(choice1 ==="paper"){
        if(choice2 ==="rock")
        return("paper wins");
}
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");{ -- on this there is semicolon after elseif block.. and how come else if is there after else block.. 
    if(choice2 ==="rock")
    return("paper wins");
}
else{
    return"scissors wins";
}
}

I am assuming you are a beginner. Write a very clean code, take care of spaces and tabs, it is the best way to solve your debugging problems. there is indeed problem on line 22, you have put a semi-colon after a conditional statement.

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"){//here the error was.
        if(choice2 === "rock")
            return("paper wins");
    } else {
        return "scissors wins";
    }
}

I'm seeing several syntax errors in your code. It should look like the following:

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

You cant have an else if after an else statement, thats what is causing your error message error(Expected an identifier and instead saw 'else') And of course no semicolorn before the line really ends

https://jsfiddle.net/8hcpfnhw/

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 if(choice1 === "paper"){ // I changed this to else if instead of else
    return "paper wins";
}
else if(choice1 ==="paper") {
    if(choice2 ==="rock")
return("paper wins");
}
else{ // only else as last check
    return"scissors wins";
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM