简体   繁体   中英

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

// Determine if the persons age is >= 13 or they are accompanied

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

Check below code snippet,

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

Your code works perfectly, but you missed some semicolons ; , and forgot to close a curly bracket } at the very end of your else statement.

While semicolons are not required for the code to work, you shouldn't write JavaScript code without using them:

  • semicolons are a good practice for "clean" programming
  • automatic semicolon injection might not work sometimes, so missing semicolons would cause unexpected behavior

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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