Skip to content

Commit 496a3c3

Browse files
authored
Merge pull request #443 from nik-contrib/feat/exercise_15
feat(totalIntegers): create exercise
2 parents ba58d6f + fe07a8c commit 496a3c3

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed

15_totalIntegers/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Exercise 15 - totalIntegers
2+
3+
Write a function that takes in an arbitrarily deep array or object and returns the total number of integers stored inside this array or object.
4+
5+
```javascript
6+
totalIntegers([[[5], 3], 0, 2, ['foo'], [], [4, [5, 6]]]); // returns 7
7+
totalIntegers({ a: 1, b: { a: [5, 10], b: 11 } }); // returns 4
8+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// The extra null check is required since typeof null === "object" evaluates to true
2+
const isObject = (value) => typeof value === 'object' && value !== null;
3+
4+
const totalIntegers = function (obj) {
5+
let count = 0;
6+
7+
if (!isObject(obj)) {
8+
return;
9+
}
10+
11+
const elements = Object.values(obj);
12+
13+
for (const el of elements) {
14+
if (Number.isInteger(el)) {
15+
count++;
16+
} else if (isObject(el)) {
17+
count += totalIntegers(el);
18+
}
19+
}
20+
return count;
21+
};
22+
23+
// Do not edit below this line
24+
module.exports = totalIntegers;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const totalIntegers = require('./totalIntegers-solution');
2+
3+
describe('totalIntegers', () => {
4+
test('Works with a simple array of numbers', () => {
5+
expect(totalIntegers([1, 2, 3])).toBe(3);
6+
});
7+
test('Ignores non-integer values', () => {
8+
expect(totalIntegers([1, 2, '3', 4])).toBe(3);
9+
});
10+
test('Works with simple objects', () => {
11+
expect(totalIntegers({ a: 1, b: '2', c: 3 })).toBe(2);
12+
});
13+
test('Works with an empty nested array', () => {
14+
expect(totalIntegers([[], [], []])).toBe(0);
15+
});
16+
test('Works with a very nested array', () => {
17+
expect(totalIntegers([[[[[[[[[[[[[[4]]]]]], 246]]]]]]]])).toBe(2);
18+
});
19+
test('Works with negative numbers', () => {
20+
expect(totalIntegers([5, 7, -7, [45, -1, -0], [4, 7, -4, -4, -4, [777777, -45674]], [-5477654]])).toBe(14);
21+
});
22+
test('Works with floats', () => {
23+
expect(totalIntegers([5, 7.7, 7, [45, 1, 0], [4.0, 7, [7.77777, 4567.4]], [5477.654]])).toBe(7);
24+
});
25+
test('Only accepts arrays or objects', () => {
26+
expect(totalIntegers('2')).toBe(undefined);
27+
expect(totalIntegers(() => {})).toBe(undefined);
28+
expect(totalIntegers(42)).toBe(undefined);
29+
expect(totalIntegers(NaN)).toBe(undefined);
30+
});
31+
test('Works with NaN', () => {
32+
expect(totalIntegers([5, NaN, [NaN, NaN, 64], 4])).toBe(3);
33+
});
34+
test('Works with a nested array of all kinds of things', () => {
35+
expect(totalIntegers([NaN, [[{}], 555 ], '444', [], 74.0, undefined, [[() => {}], [4], Infinity, [[[], -44.0], [null, '-4'], NaN [[]], 6]], () => {}, [[], [-Infinity, ['4'], [4.7, -46.7], NaN]]])).toBe(5);
36+
});
37+
test('Works with arrays containing objects containing integers', () => {
38+
expect(totalIntegers([4, 6, { a: 1, b: { a: [5, 10], b: 11 } }, 9])).toBe(7);
39+
});
40+
});

15_totalIntegers/totalIntegers.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const totalIntegers = function() {
2+
3+
};
4+
5+
// Do not edit below this line
6+
module.exports = totalIntegers;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const totalIntegers = require('./totalIntegers');
2+
3+
describe('totalIntegers', () => {
4+
test('Works with a simple array of numbers', () => {
5+
expect(totalIntegers([1, 2, 3])).toBe(3);
6+
});
7+
8+
test.skip('Ignores non-integer values', () => {
9+
expect(totalIntegers([1, 2, '3', 4])).toBe(3);
10+
});
11+
12+
test.skip('Works with simple objects', () => {
13+
expect(totalIntegers({ a: 1, b: '2', c: 3 })).toBe(2);
14+
});
15+
16+
test.skip('Works with an empty nested array', () => {
17+
expect(totalIntegers([[], [], []])).toBe(0);
18+
});
19+
20+
test.skip('Works with a very nested array', () => {
21+
expect(totalIntegers([[[[[[[[[[[[[[4]]]]]], 246]]]]]]]])).toBe(2);
22+
});
23+
24+
test.skip('Works with negative numbers', () => {
25+
expect(totalIntegers([5, 7, -7, [45, -1, -0], [4, 7, -4, -4, -4, [777777, -45674]], [-5477654]])).toBe(14);
26+
});
27+
28+
test.skip('Works with floats', () => {
29+
expect(totalIntegers([5, 7.7, 7, [45, 1, 0], [4.0, 7, [7.77777, 4567.4]], [5477.654]])).toBe(7);
30+
});
31+
32+
test.skip('Only accepts arrays or objects', () => {
33+
expect(totalIntegers('2')).toBe(undefined);
34+
expect(totalIntegers(() => {})).toBe(undefined);
35+
expect(totalIntegers(42)).toBe(undefined);
36+
expect(totalIntegers(NaN)).toBe(undefined);
37+
});
38+
39+
test.skip('Works with NaN', () => {
40+
expect(totalIntegers([5, NaN, [NaN, NaN, 64], 4])).toBe(3);
41+
});
42+
43+
test.skip('Works with a nested array of all kinds of things', () => {
44+
expect(totalIntegers([NaN, [[{}], 555 ], '444', [], 74.0, undefined, [[() => {}], [4], Infinity, [[[], -44.0], [null, '-4'], NaN [[]], 6]], () => {}, [[], [-Infinity, ['4'], [4.7, -46.7], NaN]]])).toBe(5);
45+
});
46+
47+
test.skip('Works with arrays containing objects containing integers', () => {
48+
expect(totalIntegers([4, 6, { a: 1, b: { a: [5, 10], b: 11 } }, 9])).toBe(7);
49+
});
50+
});

0 commit comments

Comments
 (0)