Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTherman Trotman
Courses Plus Student 376 Points"Help me fix this program!"
How come the first alert "help me fix this program" didn't work until the second alert was fixed? I thought the script is read in order, from top to bottom?
2 Answers
Brandon Keene
7,217 PointsAs I understand it, all the parameters passed to the functions are initialized before any of them are executed. So let's take a look at the code:
alert("Help me fix this program!");
alert("Can you get this message to appear?");
document.write("<h2>My first JavaScript program</h2>");
document.write("<p>I'm practicing 'debugging'.</p>");
Now remove any one of the " marks from inside the parantheses from any one line of the code, and none of it will execute. For example, stripping out the final " mark from the final line like so:
document.write("<p>I'm practicing 'debugging'.</p>);
will result in neither the alerts nor the document.writes running. However, if you make a mistake in the actual writing of the function, like missing a t in alert or a w in document.write, the code will execute until that line is encountered. For example, the following code will show both alerts and the first document.write:
alert("Help me fix this program!");
alert("Can you get this message to appear?");
document.write("<h2>My first JavaScript program</h2>");
document.rite("<p>I'm practicing 'debugging'."</p>");
Why is this? Like I said earlier, all the params are being initialized first, so as I understand it, all the params get checked for validity, and the system throws an error if any one of them is invalid. This error happens before any of the code is executed, and since running the code halts at an error, none of the code runs.
Conversely, if all the params are valid, it does run each line, in order, and only halts if there is an error in the exectution of a line.
That's my understanding, anyway. Hope this helps!
Therman Trotman
Courses Plus Student 376 PointsYour explanation makes perfect sense, but I'm just surprised to learn it works that way.
Brandon Keene
7,217 PointsYou're not alone; it surprised me too!