文本冒险中的js语法错误

时间:2014-06-17 19:21:35

标签: javascript syntax

嗨,我是java脚本的新手,我为学校项目写了一篇文章冒险,当我尝试运行时,我一直收到这些错误

js: "adventure.js", line 4: missing ( before condition
js: if var start = "turn on lights" || "light switch" 
js: ......^
js: "adventure.js", line 7: missing ( before condition
js: if var start = "open window" || "window"
js: ......^
js: "adventure.js", line 10: missing ( before condition
js: if var start = "wait"
js: ......^
js: "adventure.js", line 13: missing ( before condition
js: if var start = "opendoor"
js: ......^
js: "adventure.js", line 18: missing ( before condition
js: if secoundAct = "open left door" || "open door left" || "left door" || "   enter left door"
js: .............^
js: "adventure.js", line 21: missing ( before condition
js: if secoundAct = "open right" || "enter right" || "enter right door"|| "open right door"
js: .............^
js: "adventure.js", line 24: missing ( before condition
js: if secoundAct = "go back" || "turn around"
js: .............^
js: "adventure.js", line 29: missing ( before condition
js: if thirdAct = "go back" || "turn back"
js: ...........^
js: "adventure.js", line 32: missing ( before condition
js: if thirdAct = "go down right stairs" || "go right" || "right" || "down right" || "down left" || "go left" || "go down left stairs" || "jump over balcony" || "hop over balcony" 
js: ...........^
js: "adventure.js", line 37: missing ( before condition
js: if fourthAct = "monster lodging" || "go into monster lodging" || "go into giant death monnster lodging" 
js: ............^
js: "adventure.js", line 40: missing ( before condition
js: if fourthAct = "go into escape pods" || "escape pods" 
js: ............^
js: "adventure.js", line 1: Compilation produced 11 syntax errors.

有谁知道我做错了什么 如果代码有帮助https://drive.google.com/file/d/0Bz_LEt8rxihuNGdUUUluelRhQ1E/edit?usp=sharing

,请注意

1 个答案:

答案 0 :(得分:0)

在本声明中

if var start = "turn on lights" || "light switch"
  1. 您使用var声明变量。你不需要这个。
  2. 如果您正在尝试进行相等检查,请使用===。正如所写,表达式试图分配“打开灯”或(双管||)到var start。这没有意义 - 表达式总是会评估为真。
  3. 请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators了解原因。 “打开灯”不是null或空字符串,所以是真的。这同样适用于“灯开关”。在上面的例子中

    "turn on lights" || "light switch"
    

    永远是真的。

    1. 错误消息告诉您很多需要。他们说他们希望看到(。用括号括起你的条件。
    2. 所有这一切的网络,你的陈述应该是类似

      的内容
      if(start === "turn on lights" || start === "light switch"){
        // do something
      }
      

      请注意,你需要在OR的任意一侧说start ===,你不能用它来快捷。