Published at Sep 26 2019
·
0 comments

Instructions

Test suite

Solution

Given a number, find the sum of all the unique multiples of particular numbers up to but not including that number.

If we list all the natural numbers below 20 that are multiples of 3 or 5, we get 3, 5, 6, 9, 10, 12, 15, and 18.

The sum of these multiples is 78.

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

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

Install assignment dependencies:

```
$ yarn install
```

Execute the tests with:

```
$ yarn 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 `xit`

to
`it`

.

A variation on Problem 1 at Project Euler http://projecteuler.net/problem=1

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

```
import SumOfMultiples from './sum-of-multiples'
describe('SumOfMultiples', () => {
it('to 1', () => {
expect(SumOfMultiples([3, 5]).to(1)).toBe(0)
})
xit('to 3', () => {
expect(SumOfMultiples([3, 5]).to(4)).toBe(3)
})
xit('to 10', () => {
expect(SumOfMultiples([3, 5]).to(10)).toBe(23)
})
xit('to 100', () => {
expect(SumOfMultiples([3, 5]).to(100)).toBe(2318)
})
xit('to 1000', () => {
expect(SumOfMultiples([3, 5]).to(1000)).toBe(233168)
})
xit('[7, 13, 17] to 20', () => {
expect(SumOfMultiples([7, 13, 17]).to(20)).toBe(51)
})
xit('[4, 6] to 15', () => {
expect(SumOfMultiples([4, 6]).to(15)).toBe(30)
})
xit('[5, 6, 8] to 150', () => {
expect(SumOfMultiples([5, 6, 8]).to(150)).toBe(4419)
})
xit('[43, 47] to 10000', () => {
expect(SumOfMultiples([43, 47]).to(10000)).toBe(2203160)
})
})
```

```
"use strict";
class SumOfMultiples {
/**
* @param {Array<number>} baseNumbers
*/
public constructor(private readonly baseNumbers: Array<number>) {
}
/**
* @param {number} factor
* @param {number} currentNumber
* @returns {boolean}
*/
private isFactorOf(factor: number, currentNumber: number): boolean {
return factor > 0 && currentNumber % factor == 0;
}
/**
* @param {number} currentNumber
* @returns {boolean}
*/
private isMultipleOfAny(currentNumber: number): boolean {
return this.baseNumbers
.filter((factor: number): boolean => this.isFactorOf(factor, currentNumber))
.length > 0;
}
/**
* @param {number} maxNumber
* @returns {number}
*/
public to(maxNumber: number): number {
return [...Array<number>(maxNumber).keys()]
.filter((currentNumber: number) : boolean => this.isMultipleOfAny(currentNumber))
.reduce((sum: number, value: number): number => sum + value, 0);
}
}
/**
* @param {Array<number>} baseNumbers
* @returns {SumOfMultiples}
*/
export default (baseNumbers: Array<number>): SumOfMultiples => new SumOfMultiples(baseNumbers);
```

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

## Community comments