我一直得到响应“意外令牌”这段代码。有没有人有建议? [英] I keep getting the response "Unexpected Token else" to this code. Does anyone have a suggestion?

查看:64
本文介绍了我一直得到响应“意外令牌”这段代码。有没有人有建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function myChildren(name){

if(name =Dave)

返回Oldest

if(name =米歇尔)

返回中间;

}



else {

返回不是我的孩子!;

}

function myChildren(name){
if (name = "Dave")
return "Oldest"
if (name = "Michelle")
return "Middle";
}

else {
return "Not my kid!";
}

推荐答案

在我看来,你的其他块就是你职能的外部。 />


这和RD上面提到的缺失分号。
Looks to me like your else block is OUTSIDE of your function.

That and the missing semicolon noted above by RD.


我认为这个代码块应该更好地阅读:

I think this code block should better read:
function myChildren(name) {
   if (name == "Dave") {
      return "Oldest";
   }
   else if (name == "Michelle") {
      return "Middle";
   }
   else {
      return "Not my kid!";
   }
} 



当您想要快速检测错位的括号时,首先要正确理解您的代码。

您还应该尝试与块的使用方式保持一致。对于前两个,你不使用大括号,而你将它们用于else部分。

我也纠正了你的平等测试;据我所知,javascript中的条件运算符是 == ,而不是 = = 是赋值运算符。



希望这有帮助。


Identing your code correctly is the first thing to do when you want to quickly detect misplaced braces.
You should also try to be consistent with the way you are using if blocks. For the two first ones, you do not use braces, whereas you use them for the else part.
I also corrected your equality tests; as far as I know, conditional operator in javascript is ==, not =. = is the assignment operator.

Hope this helps.


这篇关于我一直得到响应“意外令牌”这段代码。有没有人有建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆