你能告诉我这个javascript代码有什么问题吗?

时间:2018-05-14 03:52:23

标签: javascript

//确定年龄的人是> = 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")

2 个答案:

答案 0 :(得分:0)

检查以下代码段,

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")
    }

答案 1 :(得分:0)

您的代码效果很好,但是您错过了一些分号;,并且忘记在}语句的最末端关闭一个大括号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");
}