Java Reference
In-Depth Information
src/numberCruncher.js (incomplete, excerpt)
function isPrime(n) {
return factorsOf(n).length === 2
Now if we refresh the SpecRunner.html page, we should receive a message telling us that
all three of our specs have passed, similar to the screenshot in Figure 10.6 .
Figure 10.6. Back to the winners' list
Great, our library of functions is growing! The next step is to once again refactor our code.
It's a bit brittle at the moment, because both functions accept negative and non-integer val-
ues, which have no factors and aren't prime. It turns out that the factorsOf() function
fails silently and returns an empty array if either of these are passed. It would be better to
throw an exception to indicate that an incorrect argument has been used. Let's create some
specs to check that this happens. Open up numberSpec.js and add the following function at
the top of the file:
spec/numberSpec.js (excerpt)
var int, negative, decimal;
beforeEach(function() {
int = Math.floor(100*Math.random());
Search WWH ::

Custom Search