Java Reference
In-Depth Information
Quiz Ninja Project
We're now going to use some of the techniques we've learned in this chapter to improve the
quality of our code in the Quiz Ninja project, making it easier to debug. First of all, we'll
ensure that we use strict mode by wrapping all the code inside an immediately invoked func-
tion expression that's anonymous. To do this, place this line of code on the very first line of
the scripts.js file:
(function () {
Then place the following line on the very last line of the file:
This function call is invoked as soon as the file loads, running the code exactly the same way
as usual; however, now all the variables are wrapped up within the scope of this anonymous
function. So if this file is used in conjunction with another JavaScript file, there will be no
problems if any variables in either file share the same name.
We now should add the "use strict" declaration on the second line of the file:
"use strict";
This ensures that errors will be thrown if there are mistakes in our code, rather than just fail-
ing silently.
The next task is to use the console.log() method to log when some of the important
functions are called. The main functions in the game are chooseQuestion() , ask() ,
check() , and gameOver() . Add the following lines of code to the beginning of the rel-
evant functions:
console.log("chooseQuestion() invoked");
console.log("ask() invoked");
console.log("check() invoked");
Search WWH ::

Custom Search