Skip to content

Commit 66de414

Browse files
committed
feat/ exercise now accepts objects
1 parent e326ec4 commit 66de414

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

15_totalIntegers/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Exercise 15 - totalIntegers
22

3-
Write a function that when given a multi-dimensional integer array, return the total number of integers stored inside this array
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.
44

55
```javascript
66
totalIntegers([[[5], 3], 0, 2, ['foo'], [], [4, [5, 6]]]); // returns 7
7+
totalIntegers({ a: 1, b: { a: [5, 10], b: 11 } }); // returns 4
78
```

15_totalIntegers/solution/totalIntegers-solution.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
1-
const isInteger = (number) => {
2-
switch (true) {
3-
case (Number.isNaN(number)):
4-
case (typeof number !== 'number'):
5-
case (number % 1 !== 0):
6-
return false;
7-
default:
8-
return true;
9-
}
10-
};
1+
const totalIntegers = function(obj, count = 0) {
2+
3+
if (typeof obj !== 'object' || obj === null) return;
4+
5+
const elements = Array.isArray(obj) ? obj : Object.values(obj)
116

12-
const totalIntegers = function(array, count = 0) {
13-
if (!Array.isArray(array)) return;
14-
for (const el of array) {
15-
if (isInteger(el)) ++count;
16-
if (Array.isArray(el)) count += totalIntegers(el);
7+
for (const el of elements) {
8+
if (Number.isInteger(el)) ++count;
9+
if (typeof el === 'object' && el !== null) count += totalIntegers(el);
1710
}
1811
return count;
1912
};

15_totalIntegers/solution/totalIntegers-solution.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ describe('totalIntegers', () => {
1313
test('Works with floats', () => {
1414
expect(totalIntegers([5, 7.7, 7, [45, 1, 0], [4.0, 7, [7.77777, 4567.4]], [5477.654]])).toBe(7);
1515
});
16-
test('Only accepts arrays', () => {
16+
test('Only accepts arrays or objects', () => {
1717
expect(totalIntegers('2')).toBe(undefined);
18-
expect(totalIntegers({})).toBe(undefined);
1918
expect(totalIntegers(() => {})).toBe(undefined);
2019
expect(totalIntegers(42)).toBe(undefined);
2120
expect(totalIntegers(NaN)).toBe(undefined);
@@ -26,4 +25,7 @@ describe('totalIntegers', () => {
2625
test('Works with a nested array of all kinds of things', () => {
2726
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);
2827
});
28+
test('Works with arrays containing objects containing integers', () => {
29+
expect(totalIntegers([4, 6, { a: 1, b: { a: [5, 10], b: 11 } }, 9])).toBe(7);
30+
});
2931
});

0 commit comments

Comments
 (0)