3D Game Programming

(C. Jardin) #1
Let’s put this in action by writing some more bad JavaScript (but not too bad).
First, remove the bad()javascript line from the previous section, and add the fol-
lowing lines:

food;
eat(food);

In this case, ICE will tell us via the yellow triangle that the food line is not
doing anything.

We can fix the problem by changing the food line into an assignment, like this:


food ='Cookie';
eat(food);

ICE should accept the new food line and no longer display any errors. However,
even though ICE may not report any more issues, there is still something
wrong with this code.

2.5 Debugging in the Console


This is where the JavaScript console comes in handy, as we get to see what
the program is actually doing. Once you open up the console, you’ll see an
error message that eat() is not defined.

When the browser tried to run the bad JavaScript code, it realized there was
a problem. In our program, we told the browser to run the eat() function, but
we never told the browser how to do that. Errors found when trying to run
the code are called run-time errors.

We’ll talk more about functions in Chapter 5, Functions: Use and Use Again,
on page 49. For now, it’s enough to know that a function is a way to write
code that can be run again and again.

The errors flagged by ICE with the red X and yellow triangle are called compile-
time errors. Compile-time errors are caught when the computer is reading
the code and deciding what to do with it. Compiling refers to the computer
deciding what to do with the code.

The JavaScript console helps us fix run-time errors.


Chapter 2. Playing with the Console and Finding What’s Broken • 20


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf