-
Notifications
You must be signed in to change notification settings - Fork 39.2k
feat(totalIntegers): create exercise #443
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Open
nik-rev
wants to merge
17
commits into
TheOdinProject:main
Choose a base branch
from
nik-contrib:feat/exercise_15
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
0fd7d68
feat/ add new exercise
nik-rev bdce415
feat/ update readme
nik-rev 49da653
feat/ add tests
nik-rev 73a0f10
feat/ add solution
nik-rev e326ec4
feat/ fix test not working
nik-rev 66de414
feat/ exercise now accepts objects
nik-rev 99f8a40
fix/ semicolon
nik-rev afec252
Update 15_totalIntegers/solution/totalIntegers-solution.js
nik-rev d27fab0
Update 15_totalIntegers/solution/totalIntegers-solution.js
nik-rev 507c436
Update 15_totalIntegers/solution/totalIntegers-solution.js
nik-rev d6b751c
feat: make solution code more dry
nik-rev f95b39c
feat: add some basic test suites (Mao)
nik-rev 0ad516b
feat(totalIntegers): define count as a variable using let in the body…
nik-rev 99ebb57
chore(totalIntegers): move helper function outside to prevent it bein…
nik-rev 3efb3f9
feat(totalIntegers): use helper function instead of inlining logic
nik-rev c867d73
feat(totalIntegers): replace comment about best practice with reminde…
nik-rev fe07a8c
feat(totalIntegers): copy tests over from solution file
nik-rev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Exercise 15 - totalIntegers | ||
|
||
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 | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// 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) { | ||
let count = 0; | ||
|
||
if (!isObject(obj)) { | ||
return; | ||
} | ||
|
||
const elements = Object.values(obj); | ||
|
||
for (const el of elements) { | ||
if (Number.isInteger(el)) { | ||
count++; | ||
} else if (isObject(el)) { | ||
count += totalIntegers(el); | ||
} | ||
} | ||
return count; | ||
}; | ||
|
||
// Do not edit below this line | ||
module.exports = totalIntegers; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
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); | ||
}); | ||
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 or objects', () => { | ||
expect(totalIntegers('2')).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); | ||
}); | ||
test('Works with arrays containing objects containing integers', () => { | ||
expect(totalIntegers([4, 6, { a: 1, b: { a: [5, 10], b: 11 } }, 9])).toBe(7); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const totalIntegers = require('./totalIntegers'); | ||
|
||
describe('totalIntegers', () => { | ||
test('Works with a simple array of numbers', () => { | ||
expect(totalIntegers([1, 2, 3])).toBe(3); | ||
}); | ||
|
||
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); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.