 # piotr-jelinski's solution

## to Sum Of Multiples in the TypeScript Track

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.

## 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)
})
})``````
``````"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);``````