Welcome to the Treehouse Community
Looking to learn something new?
"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?
Brandon Keene7,217 Points
As 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:
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:
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!
Your explanation makes perfect sense, but I'm just surprised to learn it works that way.