Avatar of calebpollman

calebpollman's solution

to Sum Of Multiples in the TypeScript Track

Published at Sep 22 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)
  })
})
const SumOfMultiples = (multiples: number[]) => {
  const getMultipleValues = (multiple: number, toValue: number, multipleValues: number[]) => {
    for (let targetValue = 1; targetValue < toValue; targetValue++) {
      const isDivisibleByMultiple: boolean = targetValue % multiple === 0;
      if (isDivisibleByMultiple && !multipleValues.includes(targetValue)) {
        multipleValues.push(targetValue);
      }
    }
  };
  return {
    to: (toValue: number) => {
      let multipleValues: number[] = [];
      multiples.forEach(multiple => {
        if (multiple < toValue) getMultipleValues(multiple, toValue, multipleValues);
      });
      return multipleValues.reduce((total, multipleValue) => total + multipleValue, 0);
    },
  };
};

export default SumOfMultiples;

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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?