Javascript Age Document.write,帮助打印出18个及以上和50个以上的语句?

时间:2016-04-11 22:07:46

标签: javascript

我需要帮助根据输入年龄打印消息。如果我年满55岁,它会打印出来#34;你可以开车了!"和#34;必须进行体检。"如果我年满20岁,它只会打印第一条消息。

这是我到目前为止编写的代码:

<script>
function Driving() {
var Drive;
age = prompt("Please, put in your age");


if (age >= 18){
    document.write("You're okay to drive!");
}

else if (age == 50){
document.write("Must have medical checkup.");

} else {
    document.write("Too young to drive.");
    }
return;
}
</script>

</head>
<body onLoad="Driving()">

</body>
</html>

1 个答案:

答案 0 :(得分:0)

你必须嵌套if语句。

if (age >= 18) {
  document.write("Your okay to drive!<br>");
  if (age >= 50) {
    document.write("Must have medical checkup.");
  }
} else {
  document.write("Too young to drive.");
}

https://jsfiddle.net/cowdd/1hsjy8j3/