简体   繁体   English

无法弄清楚这个简单的JS代码出了什么问题

[英]Can't figure out what's wrong with this simple JS code

Title is self explanatory i have this JS code (going back to basics) extremely simple but i can't for the life of me figure out why i won't run. 标题是不言自明的,我有这个JS代码(回到基础知识)非常简单,但是我无法终生弄清楚为什么我不运行。 All i know is the developer console says unexpected token "{" on line 21, but that's what opens my if statement. 我所知道的是开发人员控制台在第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> 

Your missing a parenthesis on this line 您在此行上缺少括号

if ((myAge >= 30 && myAge <= 39 || (myAge >= 80 && myAge <= 89)) {

should be 应该

if ((myAge >= 30 && myAge <= 39) || (myAge >= 80 && myAge <= 89)) {

change 更改

if ( (myAge >= 30 && myAge <= 39 || (myAge >= 80 && myAge <= 89) ) {

to

if ( (myAge >= 30 && myAge <= 39) || (myAge >= 80 && myAge <= 89) ) {

or if you like, keyword operator precedence : 或者,如果您愿意,可以使用关键字运算符优先级

if (myAge >= 30 && myAge <= 39 || myAge >= 80 && myAge <= 89) {

and a little hint 和一点提示

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM