Java Reference
In-Depth Information
if (n/i === Math.floor(n/i)){
return factors;
Now if you refresh the SpecRunner.html page, you should get a nice page confirming that
our test has passed, similar to the screenshot in Figure 10.4 .
Figure 10.4. Success at last!
Our test passed, and Jasmine gives us some useful feedback about what our code does. But
just because our test passed, doesn't mean we can stop there. There is still one more step of
the TDD cycle: refactoring.
There are a few places where we can tidy the code up. First of all we should only really be
testing for factors up to the square root because if n/i is a whole number, not only is i a
factor, but n/i will be too. This will cut down the number of steps in the for loop dra-
matically. Secondly, the test to see if i is a factor of n can be written more succinctly using
the % operator. If i is a factor of n , then n%i will equal 0 because there's no remainder.
src/numberCruncher.js (incomplete)
function factorsOf(n) {
var factors = [];
Search WWH ::

Custom Search