From 0fd7d6831b6775a4ef2f2c03a64f420096cc86d1 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sat, 23 Mar 2024 14:55:19 +0000 Subject: [PATCH 01/17] feat/ add new exercise --- 15_totalIntegers/README.md | 3 +++ .../solution/totalIntegers-solution.js | 6 ++++++ .../solution/totalIntegers-solution.spec.js | 15 +++++++++++++++ 15_totalIntegers/totalIntegers.js | 6 ++++++ 15_totalIntegers/totalIntegers.spec.js | 15 +++++++++++++++ 5 files changed, 45 insertions(+) create mode 100644 15_totalIntegers/README.md create mode 100644 15_totalIntegers/solution/totalIntegers-solution.js create mode 100644 15_totalIntegers/solution/totalIntegers-solution.spec.js create mode 100644 15_totalIntegers/totalIntegers.js create mode 100644 15_totalIntegers/totalIntegers.spec.js diff --git a/15_totalIntegers/README.md b/15_totalIntegers/README.md new file mode 100644 index 0000000000..7acd851b47 --- /dev/null +++ b/15_totalIntegers/README.md @@ -0,0 +1,3 @@ +# Exercise 13 - totalIntegers + +Description of the exercise goes here. diff --git a/15_totalIntegers/solution/totalIntegers-solution.js b/15_totalIntegers/solution/totalIntegers-solution.js new file mode 100644 index 0000000000..c6848b0ccf --- /dev/null +++ b/15_totalIntegers/solution/totalIntegers-solution.js @@ -0,0 +1,6 @@ +const totalIntegers = function() { + // Replace this comment with the solution code +}; + +// Do not edit below this line +module.exports = totalIntegers; diff --git a/15_totalIntegers/solution/totalIntegers-solution.spec.js b/15_totalIntegers/solution/totalIntegers-solution.spec.js new file mode 100644 index 0000000000..6476eace5b --- /dev/null +++ b/15_totalIntegers/solution/totalIntegers-solution.spec.js @@ -0,0 +1,15 @@ +const totalIntegers = require('./totalIntegers-solution'); + +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('Second test description', () => { + // Replace this comment with any other necessary code, and update the expect line as necessary + + expect(totalIntegers()).toBe(''); + }); +}); diff --git a/15_totalIntegers/totalIntegers.js b/15_totalIntegers/totalIntegers.js new file mode 100644 index 0000000000..d4c2486153 --- /dev/null +++ b/15_totalIntegers/totalIntegers.js @@ -0,0 +1,6 @@ +const totalIntegers = function() { + +}; + +// Do not edit below this line +module.exports = totalIntegers; diff --git a/15_totalIntegers/totalIntegers.spec.js b/15_totalIntegers/totalIntegers.spec.js new file mode 100644 index 0000000000..df93da6013 --- /dev/null +++ b/15_totalIntegers/totalIntegers.spec.js @@ -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(''); + }); +}); From bdce415f93c19093939e128466babeb96c542b89 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sat, 23 Mar 2024 16:30:05 +0000 Subject: [PATCH 02/17] feat/ update readme --- 15_totalIntegers/README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/15_totalIntegers/README.md b/15_totalIntegers/README.md index 7acd851b47..9e3539e44f 100644 --- a/15_totalIntegers/README.md +++ b/15_totalIntegers/README.md @@ -1,3 +1,7 @@ -# Exercise 13 - totalIntegers +# Exercise 15 - totalIntegers -Description of the exercise goes here. +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 +``` From 49da6535583937867b88c5f1cbf8a0e52a94dc01 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 24 Mar 2024 13:31:24 +0000 Subject: [PATCH 03/17] feat/ add tests --- .../solution/totalIntegers-solution.spec.js | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/15_totalIntegers/solution/totalIntegers-solution.spec.js b/15_totalIntegers/solution/totalIntegers-solution.spec.js index 6476eace5b..273eaf1d0f 100644 --- a/15_totalIntegers/solution/totalIntegers-solution.spec.js +++ b/15_totalIntegers/solution/totalIntegers-solution.spec.js @@ -1,15 +1,29 @@ const totalIntegers = require('./totalIntegers-solution'); 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('Works with an empty nested array', () => { + expect(totalIntegers([[], [], []])).toBe(0); }); - - test('Second test description', () => { - // Replace this comment with any other necessary code, and update the expect line as necessary - - expect(totalIntegers()).toBe(''); + 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(11); + }); + 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); }); }); From 73a0f10ec424e85b326f4d27a65a1d9e126db3a8 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 24 Mar 2024 14:27:44 +0000 Subject: [PATCH 04/17] feat/ add solution --- .../solution/totalIntegers-solution.js | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/15_totalIntegers/solution/totalIntegers-solution.js b/15_totalIntegers/solution/totalIntegers-solution.js index c6848b0ccf..200a38a2f7 100644 --- a/15_totalIntegers/solution/totalIntegers-solution.js +++ b/15_totalIntegers/solution/totalIntegers-solution.js @@ -1,6 +1,22 @@ -const totalIntegers = function() { - // Replace this comment with the solution code +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; From e326ec499029d3fa797bfe958bfbadb21432f4c8 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 24 Mar 2024 14:29:58 +0000 Subject: [PATCH 05/17] feat/ fix test not working --- 15_totalIntegers/solution/totalIntegers-solution.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/15_totalIntegers/solution/totalIntegers-solution.spec.js b/15_totalIntegers/solution/totalIntegers-solution.spec.js index 273eaf1d0f..78c42a7f3f 100644 --- a/15_totalIntegers/solution/totalIntegers-solution.spec.js +++ b/15_totalIntegers/solution/totalIntegers-solution.spec.js @@ -11,7 +11,7 @@ describe('totalIntegers', () => { 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(11); + 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); From 66de414e7f26749868f5b40dc2a5235e8f1998e5 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 24 Mar 2024 17:07:06 +0000 Subject: [PATCH 06/17] feat/ exercise now accepts objects --- 15_totalIntegers/README.md | 3 ++- .../solution/totalIntegers-solution.js | 23 +++++++------------ .../solution/totalIntegers-solution.spec.js | 6 +++-- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/15_totalIntegers/README.md b/15_totalIntegers/README.md index 9e3539e44f..48d09cda82 100644 --- a/15_totalIntegers/README.md +++ b/15_totalIntegers/README.md @@ -1,7 +1,8 @@ # Exercise 15 - totalIntegers -Write a function that when given a multi-dimensional integer array, return the total number of integers stored inside this array +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. ```javascript totalIntegers([[[5], 3], 0, 2, ['foo'], [], [4, [5, 6]]]); // returns 7 +totalIntegers({ a: 1, b: { a: [5, 10], b: 11 } }); // returns 4 ``` diff --git a/15_totalIntegers/solution/totalIntegers-solution.js b/15_totalIntegers/solution/totalIntegers-solution.js index 200a38a2f7..bcdbd7d9cc 100644 --- a/15_totalIntegers/solution/totalIntegers-solution.js +++ b/15_totalIntegers/solution/totalIntegers-solution.js @@ -1,19 +1,12 @@ -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(obj, count = 0) { + + if (typeof obj !== 'object' || obj === null) return; + + const elements = Array.isArray(obj) ? obj : Object.values(obj) -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); + for (const el of elements) { + if (Number.isInteger(el)) ++count; + if (typeof el === 'object' && el !== null) count += totalIntegers(el); } return count; }; diff --git a/15_totalIntegers/solution/totalIntegers-solution.spec.js b/15_totalIntegers/solution/totalIntegers-solution.spec.js index 78c42a7f3f..0999978ab8 100644 --- a/15_totalIntegers/solution/totalIntegers-solution.spec.js +++ b/15_totalIntegers/solution/totalIntegers-solution.spec.js @@ -13,9 +13,8 @@ describe('totalIntegers', () => { 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', () => { + test('Only accepts arrays or objects', () => { expect(totalIntegers('2')).toBe(undefined); - expect(totalIntegers({})).toBe(undefined); expect(totalIntegers(() => {})).toBe(undefined); expect(totalIntegers(42)).toBe(undefined); expect(totalIntegers(NaN)).toBe(undefined); @@ -26,4 +25,7 @@ describe('totalIntegers', () => { 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); }); + test('Works with arrays containing objects containing integers', () => { + expect(totalIntegers([4, 6, { a: 1, b: { a: [5, 10], b: 11 } }, 9])).toBe(7); + }); }); From 99f8a40bc7a22b6ccbffde3613f1b7b2cf93def5 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 24 Mar 2024 17:17:24 +0000 Subject: [PATCH 07/17] fix/ semicolon --- 15_totalIntegers/solution/totalIntegers-solution.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/15_totalIntegers/solution/totalIntegers-solution.js b/15_totalIntegers/solution/totalIntegers-solution.js index bcdbd7d9cc..db11fc44c3 100644 --- a/15_totalIntegers/solution/totalIntegers-solution.js +++ b/15_totalIntegers/solution/totalIntegers-solution.js @@ -2,7 +2,7 @@ const totalIntegers = function(obj, count = 0) { if (typeof obj !== 'object' || obj === null) return; - const elements = Array.isArray(obj) ? obj : Object.values(obj) + const elements = Array.isArray(obj) ? obj : Object.values(obj); for (const el of elements) { if (Number.isInteger(el)) ++count; From afec25222621bc73255dce0336fc8400c1d517c0 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 7 Apr 2024 02:00:08 +0100 Subject: [PATCH 08/17] Update 15_totalIntegers/solution/totalIntegers-solution.js Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com> --- 15_totalIntegers/solution/totalIntegers-solution.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/15_totalIntegers/solution/totalIntegers-solution.js b/15_totalIntegers/solution/totalIntegers-solution.js index db11fc44c3..20f085a87c 100644 --- a/15_totalIntegers/solution/totalIntegers-solution.js +++ b/15_totalIntegers/solution/totalIntegers-solution.js @@ -2,7 +2,7 @@ const totalIntegers = function(obj, count = 0) { if (typeof obj !== 'object' || obj === null) return; - const elements = Array.isArray(obj) ? obj : Object.values(obj); + const elements = Object.values(obj); for (const el of elements) { if (Number.isInteger(el)) ++count; From d27fab0de44d2160a019db259506c9e1a3e09029 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 7 Apr 2024 02:01:06 +0100 Subject: [PATCH 09/17] Update 15_totalIntegers/solution/totalIntegers-solution.js Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com> --- 15_totalIntegers/solution/totalIntegers-solution.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/15_totalIntegers/solution/totalIntegers-solution.js b/15_totalIntegers/solution/totalIntegers-solution.js index 20f085a87c..6ed451e76f 100644 --- a/15_totalIntegers/solution/totalIntegers-solution.js +++ b/15_totalIntegers/solution/totalIntegers-solution.js @@ -5,8 +5,11 @@ const totalIntegers = function(obj, count = 0) { const elements = Object.values(obj); for (const el of elements) { - if (Number.isInteger(el)) ++count; - if (typeof el === 'object' && el !== null) count += totalIntegers(el); + if (Number.isInteger(el)) { + count++; + } else if (typeof el === 'object' && el !== null) { + count += totalIntegers(el); + } } return count; }; From 507c436f47ad4de1c48d85098cd919e1a012c8b2 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 7 Apr 2024 02:01:20 +0100 Subject: [PATCH 10/17] Update 15_totalIntegers/solution/totalIntegers-solution.js Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com> --- 15_totalIntegers/solution/totalIntegers-solution.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/15_totalIntegers/solution/totalIntegers-solution.js b/15_totalIntegers/solution/totalIntegers-solution.js index 6ed451e76f..16157e5fd2 100644 --- a/15_totalIntegers/solution/totalIntegers-solution.js +++ b/15_totalIntegers/solution/totalIntegers-solution.js @@ -1,6 +1,8 @@ const totalIntegers = function(obj, count = 0) { - if (typeof obj !== 'object' || obj === null) return; + if (typeof obj !== 'object' || obj === null) { + return; + } const elements = Object.values(obj); From d6b751c75bc6af327dac433ad84f974b80d5516e Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 7 Apr 2024 18:45:37 +0100 Subject: [PATCH 11/17] feat: make solution code more dry --- 15_totalIntegers/solution/totalIntegers-solution.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/15_totalIntegers/solution/totalIntegers-solution.js b/15_totalIntegers/solution/totalIntegers-solution.js index 16157e5fd2..f1f47e9583 100644 --- a/15_totalIntegers/solution/totalIntegers-solution.js +++ b/15_totalIntegers/solution/totalIntegers-solution.js @@ -1,4 +1,5 @@ -const totalIntegers = function(obj, count = 0) { +const totalIntegers = function (obj, count = 0) { + const isObject = (value) => typeof value === 'object' && value !== null; if (typeof obj !== 'object' || obj === null) { return; @@ -9,7 +10,7 @@ const totalIntegers = function(obj, count = 0) { for (const el of elements) { if (Number.isInteger(el)) { count++; - } else if (typeof el === 'object' && el !== null) { + } else if (isObject(el)) { count += totalIntegers(el); } } From f95b39c94b1d10f9100fc40833bc07d17c914796 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 7 Apr 2024 18:46:33 +0100 Subject: [PATCH 12/17] feat: add some basic test suites (Mao) --- 15_totalIntegers/solution/totalIntegers-solution.spec.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/15_totalIntegers/solution/totalIntegers-solution.spec.js b/15_totalIntegers/solution/totalIntegers-solution.spec.js index 0999978ab8..1967619606 100644 --- a/15_totalIntegers/solution/totalIntegers-solution.spec.js +++ b/15_totalIntegers/solution/totalIntegers-solution.spec.js @@ -1,6 +1,15 @@ const totalIntegers = require('./totalIntegers-solution'); describe('totalIntegers', () => { + test('Works with a simple array of numbers', () => { + expect(totalIntegers([1, 2, 3])).toBe(3); + }); + test('Ignores non-integer values', () => { + expect(totalIntegers([1, 2, '3', 4])).toBe(3); + }); + test('Works with simple objects', () => { + expect(totalIntegers({ a: 1, b: '2', c: 3 })).toBe(2); + }); test('Works with an empty nested array', () => { expect(totalIntegers([[], [], []])).toBe(0); }); From 0ad516b44061ca7da660f9ddb6a41b838cc5dfac Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 18 Aug 2024 15:47:06 +0100 Subject: [PATCH 13/17] feat(totalIntegers): define count as a variable using let in the body of the function was not being passed to any function calls --- 15_totalIntegers/solution/totalIntegers-solution.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/15_totalIntegers/solution/totalIntegers-solution.js b/15_totalIntegers/solution/totalIntegers-solution.js index f1f47e9583..c043c2e49d 100644 --- a/15_totalIntegers/solution/totalIntegers-solution.js +++ b/15_totalIntegers/solution/totalIntegers-solution.js @@ -1,4 +1,5 @@ -const totalIntegers = function (obj, count = 0) { +const totalIntegers = function (obj) { + let count = 0; const isObject = (value) => typeof value === 'object' && value !== null; if (typeof obj !== 'object' || obj === null) { From 99ebb5705100fbaaa3f78a4685201dac0573c069 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 18 Aug 2024 15:49:32 +0100 Subject: [PATCH 14/17] chore(totalIntegers): move helper function outside to prevent it being recreated --- 15_totalIntegers/solution/totalIntegers-solution.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/15_totalIntegers/solution/totalIntegers-solution.js b/15_totalIntegers/solution/totalIntegers-solution.js index c043c2e49d..30429e8451 100644 --- a/15_totalIntegers/solution/totalIntegers-solution.js +++ b/15_totalIntegers/solution/totalIntegers-solution.js @@ -1,6 +1,8 @@ +// We could define this helper function inside of totalIntegers however that would not be best practice as everytime that the recursive totalIntegers is run it would re-create the isObject function when it could just re-use it. +const isObject = (value) => typeof value === 'object' && value !== null; + const totalIntegers = function (obj) { let count = 0; - const isObject = (value) => typeof value === 'object' && value !== null; if (typeof obj !== 'object' || obj === null) { return; From 3efb3f95f0c35eb59404eed23e15793b2ac8ff4c Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 18 Aug 2024 15:50:54 +0100 Subject: [PATCH 15/17] feat(totalIntegers): use helper function instead of inlining logic Co-authored-by: Josh Smith --- 15_totalIntegers/solution/totalIntegers-solution.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/15_totalIntegers/solution/totalIntegers-solution.js b/15_totalIntegers/solution/totalIntegers-solution.js index 30429e8451..93ceb9c562 100644 --- a/15_totalIntegers/solution/totalIntegers-solution.js +++ b/15_totalIntegers/solution/totalIntegers-solution.js @@ -4,7 +4,7 @@ const isObject = (value) => typeof value === 'object' && value !== null; const totalIntegers = function (obj) { let count = 0; - if (typeof obj !== 'object' || obj === null) { + if (!isObject(obj)) { return; } From c867d73b99f545a14f6bd2e8ea44edcfa125d329 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Tue, 20 Aug 2024 23:35:37 +0100 Subject: [PATCH 16/17] feat(totalIntegers): replace comment about best practice with reminder that typeof null is object --- 15_totalIntegers/solution/totalIntegers-solution.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/15_totalIntegers/solution/totalIntegers-solution.js b/15_totalIntegers/solution/totalIntegers-solution.js index 93ceb9c562..7ba19766ca 100644 --- a/15_totalIntegers/solution/totalIntegers-solution.js +++ b/15_totalIntegers/solution/totalIntegers-solution.js @@ -1,4 +1,4 @@ -// We could define this helper function inside of totalIntegers however that would not be best practice as everytime that the recursive totalIntegers is run it would re-create the isObject function when it could just re-use it. +// The extra null check is required since typeof null === "object" evaluates to true const isObject = (value) => typeof value === 'object' && value !== null; const totalIntegers = function (obj) { From fe07a8c903b5412ba714ec67f64b0de796a42481 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Tue, 20 Aug 2024 23:36:54 +0100 Subject: [PATCH 17/17] feat(totalIntegers): copy tests over from solution file --- 15_totalIntegers/totalIntegers.spec.js | 51 ++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/15_totalIntegers/totalIntegers.spec.js b/15_totalIntegers/totalIntegers.spec.js index df93da6013..d33d2293d3 100644 --- a/15_totalIntegers/totalIntegers.spec.js +++ b/15_totalIntegers/totalIntegers.spec.js @@ -1,15 +1,50 @@ 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('Works with a simple array of numbers', () => { + expect(totalIntegers([1, 2, 3])).toBe(3); }); - test.skip('Second test description', () => { - // Replace this comment with any other necessary code, and update the expect line as necessary - - expect(totalIntegers()).toBe(''); + test.skip('Ignores non-integer values', () => { + expect(totalIntegers([1, 2, '3', 4])).toBe(3); + }); + + test.skip('Works with simple objects', () => { + expect(totalIntegers({ a: 1, b: '2', c: 3 })).toBe(2); + }); + + test.skip('Works with an empty nested array', () => { + expect(totalIntegers([[], [], []])).toBe(0); + }); + + test.skip('Works with a very nested array', () => { + expect(totalIntegers([[[[[[[[[[[[[[4]]]]]], 246]]]]]]]])).toBe(2); + }); + + test.skip('Works with negative numbers', () => { + expect(totalIntegers([5, 7, -7, [45, -1, -0], [4, 7, -4, -4, -4, [777777, -45674]], [-5477654]])).toBe(14); + }); + + test.skip('Works with floats', () => { + expect(totalIntegers([5, 7.7, 7, [45, 1, 0], [4.0, 7, [7.77777, 4567.4]], [5477.654]])).toBe(7); + }); + + test.skip('Only accepts arrays or objects', () => { + expect(totalIntegers('2')).toBe(undefined); + expect(totalIntegers(() => {})).toBe(undefined); + expect(totalIntegers(42)).toBe(undefined); + expect(totalIntegers(NaN)).toBe(undefined); + }); + + test.skip('Works with NaN', () => { + expect(totalIntegers([5, NaN, [NaN, NaN, 64], 4])).toBe(3); + }); + + test.skip('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); + }); + + test.skip('Works with arrays containing objects containing integers', () => { + expect(totalIntegers([4, 6, { a: 1, b: { a: [5, 10], b: 11 } }, 9])).toBe(7); }); });