 # freddie2025's solution

## to Prime Factors in the JavaScript Track

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.

## Example

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!

## Setup

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

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

## Requirements

Install assignment dependencies:

``````\$ npm install
``````

## Making the test suite pass

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`.

## Source

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

## Submitting Incomplete Solutions

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

### prime-factors.spec.js

``````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());

xtest('factors 3', () => expect(primeFactors(3)).toEqual());

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;
};``````