Skip to content

Commit 8a6772e

Browse files
author
Tymoteusz
authored
Merge pull request #5 from 403-html/JCC-4-add-jest-tests
[JCC-4] Add Jest tests
2 parents 47a148a + 0b4f06f commit 8a6772e

21 files changed

Lines changed: 508 additions & 216 deletions

File tree

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Node modules
22
node_modules
33

4-
# Jest coverage folder
4+
# Jest
55
coverage
6-
result.json
6+
result.json

RandSum0/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# Random N-numbers sum which is equal to 0
2-
Write a function that as an argument gets the integer N. (1 <N <100).
3-
This function is to return an array that contains unique N random numbers in the range -100 to 100, and besides that the sum of these numbers must always go to 0. For example, for N = 4 array [-1, -2.0.3]
42

5-
-------
3+
Write a function that as an argument gets the integer N `(1 < N < 100)`.
64

7-
**Enjoy**
5+
This function is to return an `array` that contains unique `N` random numbers in the range `-100` to `100`, and besides that the sum of these numbers must always go to `0`.
6+
7+
For example, for `N = 4` array `[-1, -2, 0, 3]`
8+
9+
---
10+
11+
**Enjoy**

RandSum0/__tests__/randsum.spec.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const genSum = require('../index')
2+
3+
const sumArrElems = (arr) => arr.reduce((a, b) => a + b)
4+
5+
test('Providing "0" as argument should give an array that\'s empty', () => {
6+
const arrayLength = 0
7+
const randomArray = new genSum(arrayLength).array
8+
9+
expect(randomArray.length).toBe(arrayLength)
10+
expect(randomArray).toEqual([])
11+
})
12+
13+
test('Providing "1" as argument should give an array that contains "0"', () => {
14+
const arrayLength = 1
15+
const randomArray = new genSum(arrayLength).array
16+
17+
expect(randomArray.length).toBe(arrayLength)
18+
expect(randomArray[0]).toEqual(0)
19+
})
20+
21+
test('Array should have length 4, and values should sum to 0', () => {
22+
const arrayLength = 4
23+
const randomArray = new genSum(arrayLength).array
24+
25+
expect(randomArray.length).toBe(arrayLength)
26+
expect(sumArrElems(randomArray)).toEqual(0)
27+
})
28+
29+
test('Array should have length 17, and values should sum to 0', () => {
30+
const arrayLength = 17
31+
const randomArray = new genSum(arrayLength).array
32+
33+
expect(randomArray.length).toBe(arrayLength)
34+
expect(sumArrElems(randomArray)).toEqual(0)
35+
})

RandSum0/index.html

Lines changed: 0 additions & 12 deletions
This file was deleted.

RandSum0/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
class genSum {
22
constructor(n) {
33
this.array = []
4-
if (n > 0) this.toss(n)
4+
if (n > 0) {
5+
if (n === 1) {
6+
this.array.push(0)
7+
} else {
8+
this.toss(n)
9+
}
10+
}
511
}
612
toss(n) {
713
if (n) {
@@ -41,3 +47,5 @@ class genSum {
4147
}
4248
}
4349
}
50+
51+
module.exports = genSum

binary-tree/README.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
# Binary tree
2+
23
You have to build a simple binary tree (there can only be a node of this tree in the console or you can plot it with HTML/CSS)
34

45
Basic principles:
5-
- left = smaller
6-
- for each node
7-
- right = greater
8-
- for each node
9-
- (optional) adding an option to automatically sort numbers from the lowest to the highest (sorting by searching the tree, not by other sorting)
10-
- they must work for all integers
11-
- adding the possibility of adding new values to the tree
6+
7+
- left = smaller
8+
- for each node
9+
- right = greater
10+
- for each node
11+
- they must work for all integers
12+
- adding the possibility of adding new values to the tree
13+
- tree should have that properties
14+
- each node should have properties
15+
- "number"
16+
- "nodeIndex"
17+
- "left"
18+
- "right"
19+
- root node should have properties
20+
- "valueIndex" (counting how many values were provided after "root" node get it's value)
1221

1322
Helpful links:
1423

1524
[What is binary tree](https://en.wikipedia.org/wiki/Binary_tree)
1625

17-
-------
26+
---
1827

1928
**Enjoy**
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const fs = require('fs')
2+
3+
const Tree = require('../tree')
4+
const fixtures = JSON.parse(fs.readFileSync(__dirname + '/fixtures.json'))
5+
6+
const getTreeOf = (arr) => {
7+
const tree = new Tree()
8+
9+
arr.forEach((elem) => tree.addValue(elem))
10+
11+
return tree
12+
}
13+
14+
test('empty input should get empty obj', () => {
15+
expect(getTreeOf(fixtures.zero.input)).toEqual(fixtures.zero.output)
16+
})
17+
18+
test('one input should match output tree', () => {
19+
expect(getTreeOf(fixtures.one.input)).toEqual(fixtures.one.output)
20+
})
21+
22+
test('one-smaller input should match output tree', () => {
23+
expect(getTreeOf(fixtures['one-smaller'].input)).toEqual(
24+
fixtures['one-smaller'].output
25+
)
26+
})
27+
28+
test('one-greater input should match output tree', () => {
29+
expect(getTreeOf(fixtures['one-greater'].input)).toEqual(
30+
fixtures['one-greater'].output
31+
)
32+
})
33+
34+
test('greater-and-lower input should match output tree', () => {
35+
expect(getTreeOf(fixtures['greater-and-lower'].input)).toEqual(
36+
fixtures['greater-and-lower'].output
37+
)
38+
})
39+
40+
test('inherit-greater input should match output tree', () => {
41+
expect(getTreeOf(fixtures['inherit-greater'].input)).toEqual(
42+
fixtures['inherit-greater'].output
43+
)
44+
})
45+
46+
test('many-values input should match output tree', () => {
47+
expect(getTreeOf(fixtures['many-values'].input)).toEqual(
48+
fixtures['many-values'].output
49+
)
50+
})
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
{
2+
"zero": {
3+
"input": [],
4+
"output": {}
5+
},
6+
"one": {
7+
"input": [1],
8+
"output": { "number": 1, "nodeIndex": 0 }
9+
},
10+
"one-smaller": {
11+
"input": [2, 1],
12+
"output": {
13+
"number": 2,
14+
"nodeIndex": 0,
15+
"valueIndex": 1,
16+
"left": { "number": 1, "nodeIndex": 1 }
17+
}
18+
},
19+
"one-greater": {
20+
"input": [2, 3],
21+
"output": {
22+
"number": 2,
23+
"nodeIndex": 0,
24+
"valueIndex": 1,
25+
"right": { "number": 3, "nodeIndex": 1 }
26+
}
27+
},
28+
"greater-and-lower": {
29+
"input": [2, 3, 1],
30+
"output": {
31+
"number": 2,
32+
"nodeIndex": 0,
33+
"valueIndex": 2,
34+
"right": { "number": 3, "nodeIndex": 1 },
35+
"left": { "number": 1, "nodeIndex": 2 }
36+
}
37+
},
38+
"inherit-lower": {
39+
"input": [4, 3, 2],
40+
"output": {
41+
"number": 4,
42+
"nodeIndex": 0,
43+
"valueIndex": 2,
44+
"left": {
45+
"number": 3,
46+
"nodeIndex": 1,
47+
"left": { "number": 2, "nodeIndex": 2 }
48+
}
49+
}
50+
},
51+
"inherit-greater": {
52+
"input": [2, 3, 4],
53+
"output": {
54+
"number": 2,
55+
"nodeIndex": 0,
56+
"valueIndex": 2,
57+
"right": {
58+
"number": 3,
59+
"nodeIndex": 1,
60+
"right": { "number": 4, "nodeIndex": 2 }
61+
}
62+
}
63+
},
64+
"many-values": {
65+
"input": [18, 5, 26, 13, 7, 30, 36, 11, 2, 40, 15, 4],
66+
"output": {
67+
"number": 18,
68+
"nodeIndex": 0,
69+
"valueIndex": 11,
70+
"left": {
71+
"number": 5,
72+
"nodeIndex": 1,
73+
"right": {
74+
"number": 13,
75+
"nodeIndex": 3,
76+
"left": {
77+
"number": 7,
78+
"nodeIndex": 4,
79+
"right": {
80+
"number": 11,
81+
"nodeIndex": 7
82+
}
83+
},
84+
"right": {
85+
"number": 15,
86+
"nodeIndex": 10
87+
}
88+
},
89+
"left": {
90+
"number": 2,
91+
"nodeIndex": 8,
92+
"right": {
93+
"number": 4,
94+
"nodeIndex": 11
95+
}
96+
}
97+
},
98+
"right": {
99+
"number": 26,
100+
"nodeIndex": 2,
101+
"right": {
102+
"number": 30,
103+
"nodeIndex": 5,
104+
"right": {
105+
"number": 36,
106+
"nodeIndex": 6,
107+
"right": {
108+
"number": 40,
109+
"nodeIndex": 9
110+
}
111+
}
112+
}
113+
}
114+
}
115+
}
116+
}

binary-tree/index.html

Lines changed: 0 additions & 12 deletions
This file was deleted.

binary-tree/tree.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Tree {
22
constructor() {}
33
addValue(value) {
4-
if (!this.number) {
4+
if (!this.number && value) {
55
this.number = value
66
this.nodeIndex = 0
7-
} else {
7+
} else if (this.number && value) {
88
if (this.valueIndex) this.valueIndex += 1
99
else this.valueIndex = 1
1010
// right, higher
@@ -39,3 +39,5 @@ class Branch {
3939
}
4040
}
4141
}
42+
43+
module.exports = Tree

0 commit comments

Comments
 (0)