🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # PatrickMcSweeny's solution

## to Difference Of Squares in the JavaScript Track

Published at Aug 23 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.

The square of the sum of the first ten natural numbers is (1 + 2 + ... + 10)² = 55² = 3025.

The sum of the squares of the first ten natural numbers is 1² + 2² + ... + 10² = 385.

Hence the difference between the square of the sum of the first ten natural numbers and the sum of the squares of the first ten natural numbers is 3025 - 385 = 2640.

You are not expected to discover an efficient solution to this yourself from first principles; research is allowed, indeed, encouraged. Finding the best algorithm for the problem is a key skill in software engineering.

## Setup

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

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

## Requirements

Install assignment dependencies:

``````\$ npm install
``````

## Making the test suite pass

Execute the tests with:

``````\$ npm 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 `xtest` to `test`.

## Source

Problem 6 at Project Euler http://projecteuler.net/problem=6

## Submitting Incomplete Solutions

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

### difference-of-squares.spec.js

``````import { Squares } from './difference-of-squares';

describe('difference-of-squares', () => {
const squares1 = new Squares(1);
const squares5 = new Squares(5);
const squares100 = new Squares(100);

describe('Square the sum of the numbers up to the given number', () => {
xtest('square of sum 1', () => {
expect(squares1.squareOfSum).toBe(1);
});

xtest('square of sum 5', () => {
expect(squares5.squareOfSum).toBe(225);
});

xtest('square of sum 100', () => {
expect(squares100.squareOfSum).toBe(25502500);
});
});

describe('Sum the squares of the numbers up to the given number', () => {
xtest('sum of squares 1', () => {
expect(squares1.sumOfSquares).toBe(1);
});

xtest('sum of squares 5', () => {
expect(squares5.sumOfSquares).toBe(55);
});

xtest('sum of squares 100', () => {
expect(squares100.sumOfSquares).toBe(338350);
});
});

describe('Subtract sum of squares from square of sums', () => {
xtest('difference of squares 1', () => {
expect(squares1.difference).toBe(0);
});

xtest('difference of squares 5', () => {
expect(squares5.difference).toBe(170);
});

xtest('difference of squares 100', () => {
expect(squares100.difference).toBe(25164150);
});
});
});``````
``````const add = (accumulator, currentValue) => accumulator + currentValue;

export class Squares {
constructor(number) {
this.range = Array.from(new Array(number), (x, i) => i + 1);
}

get sumOfSquares() {
return this.range.reduce((sum, n) => sum + n ** 2);
}

get squareOfSum() {
}

get difference() {
return this.squareOfSum - this.sumOfSquares;
}
}``````