Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(15) New exercise | Total Integers #443

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
7 changes: 7 additions & 0 deletions 15_totalIntegers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Exercise 15 - totalIntegers

Write a function that when given a multi-dimensional integer array, return the total number of integers stored inside this array

```javascript
totalIntegers([[[5], 3], 0, 2, ['foo'], [], [4, [5, 6]]]); // returns 7
```
22 changes: 22 additions & 0 deletions 15_totalIntegers/solution/totalIntegers-solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const isInteger = (number) => {
switch (true) {
case (Number.isNaN(number)):
case (typeof number !== 'number'):
case (number % 1 !== 0):
return false;
default:
return true;
}
};

const totalIntegers = function(array, count = 0) {
if (!Array.isArray(array)) return;
for (const el of array) {
if (isInteger(el)) ++count;
if (Array.isArray(el)) count += totalIntegers(el);
}
return count;
};

// Do not edit below this line
module.exports = totalIntegers;
29 changes: 29 additions & 0 deletions 15_totalIntegers/solution/totalIntegers-solution.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const totalIntegers = require('./totalIntegers-solution');

describe('totalIntegers', () => {
nikitarevenco marked this conversation as resolved.
Show resolved Hide resolved
test('Works with an empty nested array', () => {
expect(totalIntegers([[], [], []])).toBe(0);
});
test('Works with a very nested array', () => {
expect(totalIntegers([[[[[[[[[[[[[[4]]]]]], 246]]]]]]]])).toBe(2);
});
test('Works with negative numbers', () => {
expect(totalIntegers([5, 7, -7, [45, -1, -0], [4, 7, -4, -4, -4, [777777, -45674]], [-5477654]])).toBe(14);
});
test('Works with floats', () => {
expect(totalIntegers([5, 7.7, 7, [45, 1, 0], [4.0, 7, [7.77777, 4567.4]], [5477.654]])).toBe(7);
});
test('Only accepts arrays', () => {
expect(totalIntegers('2')).toBe(undefined);
expect(totalIntegers({})).toBe(undefined);
expect(totalIntegers(() => {})).toBe(undefined);
expect(totalIntegers(42)).toBe(undefined);
expect(totalIntegers(NaN)).toBe(undefined);
});
test('Works with NaN', () => {
expect(totalIntegers([5, NaN, [NaN, NaN, 64], 4])).toBe(3);
});
test('Works with a nested array of all kinds of things', () => {
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);
});
});
6 changes: 6 additions & 0 deletions 15_totalIntegers/totalIntegers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const totalIntegers = function() {

};

// Do not edit below this line
module.exports = totalIntegers;
15 changes: 15 additions & 0 deletions 15_totalIntegers/totalIntegers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const totalIntegers = require('./totalIntegers');

describe('totalIntegers', () => {
test('First test description', () => {
// Replace this comment with any other necessary code, and update the expect line as necessary

expect(totalIntegers()).toBe('');
});

test.skip('Second test description', () => {
// Replace this comment with any other necessary code, and update the expect line as necessary

expect(totalIntegers()).toBe('');
});
});