JavaScript SyntaxError:其他意外的令牌

时间:2015-12-31 20:27:08

标签: javascript

我有一个摇滚,纸张,剪刀游戏的代码,基本上是功课。我已经进行了双重检查,但似乎很好,但是,当我运行它时,它说:

SyntaxError: Unexpected token else, 

任何帮助将非常感激:)请注意我是一个新手,所以如果问题是愚蠢的,请很好,并帮助< 3

我刚刚编辑了一下代码,因为我有很多“忽视”错误。我还想澄清一下,我需要位于function语句之后的所有代码都在函数内部,这就是为什么我不立即关闭第一个{。 PD:现在我得到:SyntaxError:Unexpected token =

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)

4 个答案:

答案 0 :(得分:1)

如果您

,您会发现调试更容易
  1. 正确缩进,
  2. 除了最简单的if语句之外,使用大括号
  3. 例如:

    Cell

答案 1 :(得分:1)

始终正确格式化代码。在}语句之前,您缺少一堆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)

答案 2 :(得分:1)

考虑以更简单的方式重写它。

请参阅小提琴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");
  }

答案 3 :(得分: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!";
    }
    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)