Java Reference
In-Depth Information
factors.push(i);
}
}
return factors;
}
This function creates a local variable called
factors
and initializes it to an empty array.
It then loops through every integer value from
1
up to
n
(the number that was given as an
argument) and adds it to the array of factors using the
push()
method if it's a factor. We
test if it's a factor by seeing if the answer leaves a whole number with no remainder when
n
is divided by the integer
i
(the definition of a factor).
Try running the test again by refreshing the SpecRunner.html page. It should look similar
to the screenshot in
Figure 10.3
.