Skip to content

Commit 9d65120

Browse files
author
programmiri
committed
Add excercise and first solution
1 parent 12baac9 commit 9d65120

File tree

7 files changed

+5069
-0
lines changed

7 files changed

+5069
-0
lines changed

difference-of-squares/.eslintrc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"root": true,
3+
"parser": "babel-eslint",
4+
"parserOptions": {
5+
"ecmaVersion": 7,
6+
"sourceType": "module"
7+
},
8+
"env": {
9+
"es6": true,
10+
"node": true,
11+
"jest": true
12+
},
13+
"extends": [
14+
"eslint:recommended",
15+
"plugin:import/errors",
16+
"plugin:import/warnings"
17+
],
18+
"rules": {
19+
"linebreak-style": "off",
20+
21+
"import/extensions": "off",
22+
"import/no-default-export": "off",
23+
"import/no-unresolved": "off",
24+
"import/prefer-default-export": "off"
25+
}
26+
}

difference-of-squares/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Difference Of Squares
2+
3+
Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.
4+
5+
The square of the sum of the first ten natural numbers is
6+
(1 + 2 + ... + 10)² = 55² = 3025.
7+
8+
The sum of the squares of the first ten natural numbers is
9+
1² + 2² + ... + 10² = 385.
10+
11+
Hence the difference between the square of the sum of the first
12+
ten natural numbers and the sum of the squares of the first ten
13+
natural numbers is 3025 - 385 = 2640.
14+
15+
You are not expected to discover an efficient solution to this yourself from
16+
first principles; research is allowed, indeed, encouraged. Finding the best
17+
algorithm for the problem is a key skill in software engineering.
18+
19+
## Setup
20+
21+
Go through the setup instructions for Javascript to install the necessary
22+
dependencies:
23+
24+
[https://exercism.io/tracks/javascript/installation](https://exercism.io/tracks/javascript/installation)
25+
26+
## Requirements
27+
28+
Install assignment dependencies:
29+
30+
```bash
31+
$ npm install
32+
```
33+
34+
## Making the test suite pass
35+
36+
Execute the tests with:
37+
38+
```bash
39+
$ npm test
40+
```
41+
42+
In the test suites all tests but the first have been skipped.
43+
44+
Once you get a test passing, you can enable the next one by changing `xtest` to
45+
`test`.
46+
47+
## Source
48+
49+
Problem 6 at Project Euler [http://projecteuler.net/problem=6](http://projecteuler.net/problem=6)
50+
51+
## Submitting Incomplete Solutions
52+
53+
It's possible to submit an incomplete solution so you can see how others have
54+
completed the exercise.

difference-of-squares/babel.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
'@babel/env',
5+
{
6+
targets: {
7+
node: 'current',
8+
},
9+
useBuiltIns: false,
10+
},
11+
12+
],
13+
],
14+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export class Squares {
2+
constructor(number) {
3+
this.number = number;
4+
}
5+
6+
get sumOfSquares() {
7+
let count = 0;
8+
for (let i = 1; i <= this.number; i++ ) {
9+
count += i * i
10+
}
11+
return count;
12+
}
13+
14+
get squareOfSum() {
15+
let count = 0;
16+
for (let i = 1; i <= this.number; i++ ) {
17+
count += i
18+
}
19+
return count * count;
20+
}
21+
22+
get difference() {
23+
return this.squareOfSum - this.sumOfSquares
24+
}
25+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Squares } from './difference-of-squares';
2+
3+
describe('difference-of-squares', () => {
4+
const squares1 = new Squares(1);
5+
const squares5 = new Squares(5);
6+
const squares100 = new Squares(100);
7+
8+
describe('Square the sum of the numbers up to the given number', () => {
9+
test('square of sum 1', () => {
10+
expect(squares1.squareOfSum).toBe(1);
11+
});
12+
13+
test('square of sum 5', () => {
14+
expect(squares5.squareOfSum).toBe(225);
15+
});
16+
17+
test('square of sum 100', () => {
18+
expect(squares100.squareOfSum).toBe(25502500);
19+
});
20+
});
21+
22+
describe('Sum the squares of the numbers up to the given number', () => {
23+
test('sum of squares 1', () => {
24+
expect(squares1.sumOfSquares).toBe(1);
25+
});
26+
27+
test('sum of squares 5', () => {
28+
expect(squares5.sumOfSquares).toBe(55);
29+
});
30+
31+
test('sum of squares 100', () => {
32+
expect(squares100.sumOfSquares).toBe(338350);
33+
});
34+
});
35+
36+
describe('Subtract sum of squares from square of sums', () => {
37+
test('difference of squares 1', () => {
38+
expect(squares1.difference).toBe(0);
39+
});
40+
41+
test('difference of squares 5', () => {
42+
expect(squares5.difference).toBe(170);
43+
});
44+
45+
test('difference of squares 100', () => {
46+
expect(squares100.difference).toBe(25164150);
47+
});
48+
});
49+
});

difference-of-squares/package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "exercism-javascript",
3+
"description": "Exercism exercises in Javascript.",
4+
"author": "Katrina Owen",
5+
"private": true,
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/exercism/javascript"
9+
},
10+
"devDependencies": {
11+
"@babel/cli": "^7.5.5",
12+
"@babel/core": "^7.5.5",
13+
"@babel/preset-env": "^7.5.5",
14+
"@types/jest": "^24.0.16",
15+
"@types/node": "^12.6.8",
16+
"babel-eslint": "^10.0.2",
17+
"babel-jest": "^24.8.0",
18+
"eslint": "^6.1.0",
19+
"eslint-plugin-import": "^2.18.2",
20+
"jest": "^24.8.0"
21+
},
22+
"jest": {
23+
"modulePathIgnorePatterns": [
24+
"package.json"
25+
]
26+
},
27+
"scripts": {
28+
"test": "jest --no-cache ./*",
29+
"watch": "jest --no-cache --watch ./*",
30+
"lint": "eslint .",
31+
"lint-test": "eslint . && jest --no-cache ./* "
32+
},
33+
"license": "MIT",
34+
"dependencies": {}
35+
}

0 commit comments

Comments
 (0)