javascript - 意外的标记 else javascript

标签 javascript google-chrome

所有其他语句都会给出意外的 token 错误

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);

最佳答案

第一件事(不是语法错误,但无论如何都是错误的):使用 == 而不是 = 进行比较。

您的语法错误是您在 if block 之后使用了 ; - 删除它们。

关于javascript - 意外的标记 else javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15703537/

相关文章:

javascript - 使用 for 循环创建 div 元素

javascript - 如何在 CSS 中创建响应式 Sprite 表动画?

jquery - Chrome 跨域发布中未发送 Cookie

python - 在 Python 上使用 JSON 文件

css - FireFox 和 Chrome 在 url 中处理\(反斜杠)的不同行为

java - GWT TextArea onBlur 未被调用

javascript - 有没有更好的方法来访问使用 JQuery 选择器选择的第一个元素?

javascript - 表单验证不起作用

javascript - 为什么<内存>无法克隆?

javascript - 如何在 jquery 中使用 setTimeout 一个一个地显示我的列表组元素?