2016-04-30 120 views
-1
// Check if the user is ready to play! 

confirm("Are you ready to play?"); 

var age = prompt("What's your age"); 

if (age is less than 13) 
{ 
    console.log("You are allowed to play,but we take no responsibility"); 
} 

else { 
console.log("Go on! you can play"); 
} 
// Check if the user is ready to play! 

confirm("Are you ready to play?"); 

var age = prompt("What's your age"); 

if (age is less than 13) 
{ 
    console.log("You are allowed to play,but we take no responsibility"); 
} 

else { 
console.log("Go on! you can play"); 
} 

執行此JavaScript代碼時,出現語法錯誤,前兩行(確認和變量)正確,此錯誤位於if /其他的問題。由於if/else語句中的語法無效導致的語法錯誤

+0

第一行是在這個意義上 「正確的」在句法上是有效的,但是如果你沒有測試th,那麼在沒有意義的情況下使用confirm()是不正確的e結果看到用戶按下了哪個按鈕。此外,console.log()語句中的消息似乎是針對用戶的,但他們不會在控制檯中看到它們。 – nnnnnn

回答

2

使用<操盤is less than

// Check if the user is ready to play! 
 

 
confirm("Are you ready to play?"); 
 

 
var age = prompt("What's your age"); 
 
if (age < 13) { 
 
    alert("You are allowed to play, but we take no responsibility"); 
 
} else { 
 
    alert("Go on! you can play"); 
 
}

-1

您還可以通過使用三元運算符減少代碼行

age < 13 ? console.log("You are allowed to play, but we take no  responsibility"):console.log("Go on! you can play"); 
+1

通過將三元運算符放入一個console.log()中,可以進一步降低它。 – nnnnnn