简体   繁体   English

意外的令牌其他JavaScript

[英]unexpected token else javascript

all else statements give an unexpected token error 所有其他语句给出了意外的令牌错误

var men = 50;
alert("You are a Spartan warrior about to be fighting in the Trojan War, you get onto             your ship and set sail with a fleet of other ships but your ship breaks away from the others in a horrible storm! You have 50 men on your ship, do not lose more than 40 of them, or else.");
var start = confirm("are you ready to start?");
if (start = true) {
   alert("your platoon of Spartan soldiers is on a ship, headed for Troy");
}
else {
   alert("You failed in your mission and are executed in Sparta the next day (refresh the page)")
};
var hydra  = prompt("you encounter a large hydra on your journey to Troy, do you sail past it, or fight it?(fight or flee?)");
if (hydra = "fight") {
   alert("You kill the Hydra, but it has killed 8 of your men");
};
else {
   alert("You go around the Hydra, but it snatches up 6 of your men and starts to follow your ship!");
};
if (hydra = "fight") {
   men = 42
}
else {
   men = 44; 
};
console.log(men);

First thing (not a syntax error, but wrong anyway): Compare using == instead of = . 第一件事(不是语法错误,但还是有错误):使用==而不是=进行比较。

Your syntax error is that you're using ; 您的语法错误是您正在使用; after the if blocks – remove those. 在if块之后–删除那些。

The unexpected token error is because you end the else's with }; 意外的令牌错误是因为您用};结束了else的}; but should only be } 但只能是}

replace if (start = true) with if (start == true) instead. if (start = true)替换为if (start == true) And }; }; but should only be } 但只能是}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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