vjba's solution

to Sum Of Multiples in the TypeScript Track

Published at Sep 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.

Setup

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

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

Requirements

Install assignment dependencies:

``````\$ yarn install
``````

Making the test suite pass

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

Source

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

Submitting Incomplete Solutions

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

sum-of-multiples.test.ts

``````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)
})
})``````
``````interface sum {
to: (ceiling: number) => number
}

export default function SumOfMultiples(numbers: number[]): sum {
return {
to: (ceiling: number) => {
let result = 0
for (let i = 0; i < ceiling; i++) {
let notCounted = true
for (const num of numbers) {
if (i % num === 0 && notCounted) {
result += i
notCounted = false
}
}
}
return result
}
}
}``````