Java Reference
In-Depth Information
the SpecRunner.html page in the browser. If you then click on the “Spec List” tab you can
see the feedback about each spec. It should look similar to the screenshot in
Figure 10.7
.
Figure 10.7. An unexpected fail
Oh, no! Our tests for the
factorsOf()
function all pass ... but the exceptions have
caused the
isPrime()
function to choke and fail the tests. We need to add code that
handles any exceptions that might be thrown when the
factorsOf()
function is called
from within the
isPrime()
function. This sounds like a job for a
try
...
catch
...
fi-
nally
block. Change the
isPrime()
function in the numberCruncher.js file to the fol-
lowing:
src/numberCruncher.js
(excerpt)
function isPrime(n){
var result;
try{
result = factorsOf(n).length === 2;
} catch(e){
result = false;
} finally{
