Published at Oct 21 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)
})
})
```

```
function SumOfMultiples(numbers: number[]) {
return {
to(stop: number): number {
let sum: number = 0
for (const n of range(1, stop)) {
if (numbers.some(x => n % x === 0)) {
sum += n
}
}
return sum
}
}
}
function* range(start: number, stop: number) {
for (let i = start; i < stop; i++) {
yield i
}
}
export default SumOfMultiples
```

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