简体   繁体   中英

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. All i know is the developer console says unexpected token "{" on line 21, but that's what opens my if statement.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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