Published at Apr 14 2020
·
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:

https://exercism.io/tracks/javascript/installation

Install assignment dependencies:

```
$ npm install
```

Execute the tests with:

```
$ npm test
```

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing `xtest`

to
`test`

.

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.

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

```
//
// This is only a SKELETON file for the 'Prime Factors' exercise. It's been provided as a
// convenience to get you started writing code faster.
//
export const primeFactors = (number) => {
let list = [];
let division = 2;
do
{
if(number % division == 0)
{
list.push(division);
number = number / division;
division = 2;
}
else division++;
}
while (number > 1);
return list;
};
```

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,394 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments