簡體   English   中英

JavaScript SyntaxError:其他意外令牌

[英]JavaScript SyntaxError: Unexpected token else

我有一個基本上是家庭作業的石頭,剪刀,剪刀游戲代碼。 我已經仔細檢查過,這似乎很好,但是,當我運行它時,它說:

SyntaxError: Unexpected token else, 

任何幫助將不勝感激:)請注意,我是新手,所以如果問題很愚蠢,請幫助並幫助<3

由於我有很多“忽略”錯誤,所以我只是對代碼進行了一點編輯。 我還想澄清一下,我需要位於function語句之后的所有代碼都位於function內部,這就是為什么我不立即關閉第一個{ PD:現在我得到:SyntaxError:意外令牌=

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

            else {
                return "Paper wins"; }    
            }
        }
}

compare(userChoice, computerChoice)

如果您進行調試,會發現調試容易得多

  1. 適當縮進
  2. 除最簡單的if語句外,將括號用於

例如:

if (choice1 == choice2) return "tie";         /* simple 1-line if is ok */
if (choice1 == "rock") {
    if (choice2 == "scissors") {              /* more complex, always use braces */
        return "rock wins";                   /* always indent nicely */
    } else {
        return "paper wins";
    }
}
/* ... and so on ... */

始終正確格式化代碼。 else語句之前,您缺少一堆} 始終在行尾使用分號(不,從技術上講,這不是您的最佳做法)。

另外,您需要注意自己的平等。 您有一個====而不是===

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

compare(userChoice, computerChoice)

考慮以更簡單的方式重寫它。

參見小提琴https://jsfiddle.net/DIRTY_SMITH/c7ww2hmz/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";
  }
  alert("the computer picked " + computerChoice);
  if ((computerChoice === "rock" && userChoice === "papper") || (computerChoice === "papper" && userChoice === "scissors") || (computerChoice === "scissors" && userChoice === "rock")) {
    alert("you won");
  } else if (computerChoice == userChoice) {
    alert("It's a tie");
  } else {
    alert("you loose");
  }

好的,為了忠於您的家庭作業,我保留了相同的格式來解決問題。

這里是:

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


  }
  compare(userChoice, computerChoice)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM