[英]Can't figure out what's wrong with this simple JS code
標題是不言自明的,我有這個JS代碼(回到基礎知識)非常簡單,但是我無法終生弄清楚為什么我不運行。 我所知道的是開發人員控制台在第21行說了意外的令牌“ {”,但這就是打開我的if語句的原因。
enter code here
<!Doctype html>
<html land="en">
<head>
<meta charset="utf-8" />
<title>Chapter 2, Example 7</title>
</head>
<body>
<script type="text/javascript">
var myAge = parseInt(prompt ("Enter your age", 30),10);
if (myAge >= 0 && myAge <= 100) {
document.write("myAge is between 0 and 10<br />");
}
if (!(myAge >= 0 && myAge <= 10) ) {
document.write("myAge is NOT between 0 and 10<br />");
}
if (myAge >= 80 || myAge <= 10) {
document.write("myAge is 80 or above OR 10 or below<br />");
}
if ( (myAge >= 30 && myAge <= 39 || (myAge >= 80 && myAge <= 89) ) {
document.write("myAge is between 30 and 39 or myAge is " +
"is between 80 and 89");
}
</script>
</body>
</html>
您在此行上缺少括號
if ((myAge >= 30 && myAge <= 39 || (myAge >= 80 && myAge <= 89)) {
應該
if ((myAge >= 30 && myAge <= 39) || (myAge >= 80 && myAge <= 89)) {
更改
if ( (myAge >= 30 && myAge <= 39 || (myAge >= 80 && myAge <= 89) ) {
至
if ( (myAge >= 30 && myAge <= 39) || (myAge >= 80 && myAge <= 89) ) {
或者,如果您願意,可以使用關鍵字運算符優先級 :
if (myAge >= 30 && myAge <= 39 || myAge >= 80 && myAge <= 89) {
和一點提示
if (myAge >= 0 && myAge <= 100) { // <-- or this should be 10 ...?
document.write("myAge is between 0 and 10<br />");
} // ^^
// should be 100?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.