// 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 / else语句中。
答案 0 :(得分: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");
}
&#13;
答案 1 :(得分:-1)
您也可以使用三元运算符
来减少代码行数 age < 13 ? console.log("You are allowed to play, but we take no responsibility"):console.log("Go on! you can play");