Published at Jul 13 2018
·
0 comments

Instructions

Test suite

Solution

Compute the prime factors of a given natural number.

A prime number is only evenly divisible by itself and 1.

Note that 1 is not a prime number.

What are the prime factors of 60?

- Our first divisor is 2. 2 goes into 60, leaving 30.
- 2 goes into 30, leaving 15.
- 2 doesn't go cleanly into 15. So let's move on to our next divisor, 3.

- 3 goes cleanly into 15, leaving 5.
- 3 does not go cleanly into 5. The next possible factor is 4.
- 4 does not go cleanly into 5. The next possible factor is 5.

- 5 does go cleanly into 5.
- We're left only with 1, so now, we're done.

Our successful divisors in that computation represent the list of prime factors of 60: 2, 2, 3, and 5.

You can check this yourself:

- 2 * 2 * 3 * 5
- = 4 * 15
- = 60
- Success!

Go through the setup instructions for JavaScript to install the necessary dependencies:

http://exercism.io/languages/javascript/installation

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

```
npm install -g jasmine
```

Run the test suite from the exercise directory with:

```
jasmine prime-factors.spec.js
```

In many test suites all but the first test have been marked "pending".
Once you get a test passing, activate the next one by changing `xit`

to `it`

.

The Prime Factors Kata by Uncle Bob http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
var primeFactors = require('./prime-factors');
describe('primeFactors', function () {
it('returns an empty array for 1', function () {
expect(primeFactors.for(1)).toEqual([]);
});
xit('factors 2', function () {
expect(primeFactors.for(2)).toEqual([2]);
});
xit('factors 3', function () {
expect(primeFactors.for(3)).toEqual([3]);
});
xit('factors 4', function () {
expect(primeFactors.for(4)).toEqual([2, 2]);
});
xit('factors 6', function () {
expect(primeFactors.for(6)).toEqual([2, 3]);
});
xit('factors 8', function () {
expect(primeFactors.for(8)).toEqual([2, 2, 2]);
});
xit('factors 9', function () {
expect(primeFactors.for(9)).toEqual([3, 3]);
});
xit('factors 27', function () {
expect(primeFactors.for(27)).toEqual([3, 3, 3]);
});
xit('factors 625', function () {
expect(primeFactors.for(625)).toEqual([5, 5, 5, 5]);
});
xit('factors 901255', function () {
expect(primeFactors.for(901255)).toEqual([5, 17, 23, 461]);
});
xit('factors 93819012551', function () {
expect(primeFactors.for(93819012551)).toEqual([11, 9539, 894119]);
});
});
```

```
module.exports.for = function(num) {
var factors=[];
for (var factor=2; factor<=num/2; ++factor) {
while (!(num % factor)) {
factors.push(factor);
num /= factor;
}
if (num==1) break;
}
if (num>1) factors.push(num);
return factors;
}
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,373 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments