簡體   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