0

need help running both scripts, only one seems to run, I get the name one to work but than it just stops running. help would be appreciated. I've been using java and C++ so this is naturally confusing to me. Thanks

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <script src="jstut.js"></script>

    <style type="text/css">
      body {font-size: 1.6em;}
      .hidden {display:none;}
      .show {display:inline !important;}
      button {
        border: 2px solid black; background: #E5E4E2;
        font-size: .5em; font-weight: bold; color: black;
        padding: .8em 2em;
        margin-top: .4em;
      }
    </style>
  </head>
  <body>
    <p id="sayHello"></p>

    <script> 
      var yourName = prompt("What is your name?");
      if(yourName!= null){
        document.getElementById("sayHello").innerHTML = "Hello " + yourName;
      }else{
        alert("Please enter your name correctly");
      }
    </script>

    <script> 
      var myAge = prompt("What is your age");
      if(myAge < 4){
        document.write ("You should be in preschool";
      }else if(my age > 4 && <18){
        document.write("You should be in public private school");
      }else if (my age >18 && <24){
        document.write("You should be in college");
      }
      else{ document.write(your in the work force now);}
    </script>
  </body>
</html>
Albzi
  • 15,431
  • 6
  • 46
  • 63
  • Press F12 in your browser and look in the *Console*, there you will see an error telling you there is a missing `)` here: `("You should be in preschool";` – Alex K. Apr 12 '17 at 14:14
  • Learn to use some tools to validate your JS syntax: http://stackoverflow.com/questions/2120093/how-to-find-javascript-syntax-errors – Artur K. Apr 12 '17 at 14:14
  • Also, `document.write(your in the work force now);` should be `document.write("your in the work force now");` – Henry Apr 12 '17 at 14:14
  • 1
    @Henry Technically it should be "`you're` in the work force now." – Albzi Apr 12 '17 at 14:15
  • 1
    @Albzi Surprised you didn't point out that technically `What is your age` is missing the `?` – Henry Apr 12 '17 at 14:17
  • @Henry I have failed my family. :( – Albzi Apr 12 '17 at 14:17
  • Now im getting the error body { font-size: 1.6em; } .hidden { display: none; } .show { display: inline!important; } button { border: 2px solid black; background: #E5E4E2; font-size: .5em; font-weight: bold; color: black; padding: .8em 2em; margin-top: .4em; } – Solidswizzle Apr 12 '17 at 14:44
  • because `display: inline!important` should be `display: inline !important` – Henry Apr 12 '17 at 14:48

1 Answers1

2

You missed a ) on this line: document.write ("You should be in preschool";

There were also a few other mistakes that were pointed out by @Albzi, @Alex K., and @Henry in the comments. These guys helped a lot.

Code changes:

  • Fixed Problem with ) in this line: document.write ("You should be in preschool";
  • Replaced if(my age > 4 && <18) with if (myAge>4 && myAge<18). Same goes for the line below.
  • Replaced my age with myAge
  • Corrected spelling

body {
  font-size: 1.6em;
}

.hidden {
  display: none;
}

.show {
  display: inline!important;
}

button {
  border: 2px solid black;
  background: #E5E4E2;
  font-size: .5em;
  font-weight: bold;
  color: black;
  padding: .8em 2em;
  margin-top: .4em;
}
<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <script src="jstut.js"></script>
</head>

<body>
  <p id="sayHello"></p>

  <script>
    var yourName = prompt("What is your name?");
    if (yourName != null) {
      document.getElementById("sayHello").innerHTML = "Hello " + yourName;
    } else {
      alert("Please enter your name correctly");
    }
  </script>

  <script>
    var myAge = prompt("What is your age?");
    if (myAge < 4) {
      document.write("You should be in preschool");
    } else if (myAge > 4 && myAge < 18) {
      document.write("You should be in public private school");
    } else if (myAge > 18 && myAge < 24) {
      document.write("You should be in college");
    } else {
      document.write("You're in the work force now");
    }
  </script>
</body>

</html>
milt_on
  • 527
  • 1
  • 9
  • 20