繁体   English   中英

您能告诉我这段JavaScript代码有什么问题吗?

[英]Can you tell me what is wrong with this javascript code?

//确定年龄是否大于等于13岁或是否已陪同

var age = prompt("How old are you")
var accompanied = prompt("Are you accompanied by an adult")

if (age >= 13 || accompanied == "Yes"){
    console.log("you may see the movie")
}else{
    console.log ("You can not see the movie")

检查下面的代码片段,

 var age = Number(prompt("How old are you")); var accompanied = prompt("Are you accompanied by an adult"); if (age >= 13 || accompanied == "Yes"){ console.log("you may see the movie") }else{ console.log ("You can not see the movie") } 

您的代码运行完美,但是您错过了一些分号; ,而忘记在else语句的最后关闭大括号}

尽管代码不需要分号,但不使用分号就不应编写JavaScript代码:

  • 分号是“干净”编程的好习惯
  • 自动分号注入有时可能无法工作,因此缺少分号会导致意外行为

 var age = prompt("How old are you"); var accompanied = prompt("Are you accompanied by an adult"); if (age >= 13 || accompanied == "Yes") { console.log("you may see the movie"); } else { console.log("You can not see the movie"); } 

暂无
暂无

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

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