Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

I prefer this refactor

  • Loading branch information...
commit c15b8851bf5cd0eda96919b0cc8b3ab66ed7c55c 1 parent c80a6e9
James Sanders authored
View
8 public/katas/prime-factors/lib/prime-factors.js
@@ -1,10 +1,12 @@
(function() {
window.primeFactors = function(num) {
- var primes = [];
- for(var candidate = 2; num > 1; candidate++) {
- for(; num % candidate == 0; num /= candidate) {
+ var primes = [], candidate = 2;
+ while(num > 1) {
+ while(num % candidate == 0) {
primes.push(candidate);
+ num /= candidate;
}
+ candidate++
}
return primes;
};
View
13 public/katas/prime-factors/spec/prime-factors-spec.js
@@ -2,30 +2,43 @@ describe("#primeFactors", function() {
it("finds no factors for 1", function() {
expect(primeFactors(1)).toEqual([]);
});
+
it("finds 2 as a prime factor of 2", function() {
expect(primeFactors(2)).toEqual([2]);
});
+
it("finds 3 as a prime factor of 3", function() {
expect(primeFactors(3)).toEqual([3]);
});
+
it("finds [2, 2] as prime factors of 4", function() {
expect(primeFactors(4)).toEqual([2, 2]);
});
+
it("finds [2, 3] as prime factors of 6", function() {
expect(primeFactors(6)).toEqual([2, 3]);
});
+
it("finds [2, 2, 2] as prime factors of 8", function() {
expect(primeFactors(8)).toEqual([2, 2, 2]);
});
+
it("finds [3, 3] as prime factors of 9", function() {
expect(primeFactors(9)).toEqual([3, 3]);
});
+
it("finds [5, 2] as prime factors of 10", function() {
expect(primeFactors(10)).toEqual([2, 5]);
});
+
it("finds [11] as prime factors of 11", function() {
expect(primeFactors(11)).toEqual([11]);
});
+
+ it("finds [2, 2, 3] as prime factors of 12", function() {
+ expect(primeFactors(12)).toEqual([2, 2, 3]);
+ });
+
it("finds [2, 2, 3] as prime factors of 12", function() {
expect(primeFactors(12)).toEqual([2, 2, 3]);
});
Please sign in to comment.
Something went wrong with that request. Please try again.