-
-
Notifications
You must be signed in to change notification settings - Fork 327
London | 26-ITP-May | Zadri Abdule | Sprint 1 | Data groups #1238
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
base: main
Are you sure you want to change the base?
Changes from all commits
dd734e2
135bf30
91bb648
0505216
f1631e7
9c4d4dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,57 @@ | ||
| function dedupe() {} | ||
| /* | ||
| notes: | ||
|
|
||
| - One-line summary: return a new array with duplicates removed, preserving | ||
| the order of first occurrences. | ||
| - Input handling: null or undefined -> return [], any other non-array -> throw. | ||
| - Key idea: use a Set to track values we've seen; push unseen items to result. | ||
|
|
||
| Quick checks: | ||
| 1) If nothing changes, test with: node -e "console.log(require('./implement/dedupe')([1,1,2]))" | ||
| 2) If order looks wrong, remember we preserve first occurrences. | ||
| 3) If unexpected equality (objects), remember objects are compared by reference. | ||
|
|
||
| Tiny TODOs (one small step each): | ||
| - [ ] Consider accepting array-like objects? (optional) | ||
| - [ ] Decide whether to treat non-array values consistently (return [] vs throw) | ||
|
|
||
| Short examples: | ||
| dedupe([1,2,1]) -> [1,2] | ||
| dedupe(null) -> [] | ||
| dedupe([{a:1},{a:1}]) -> [{a:1},{a:1}] // different refs stay, not deduped by shape | ||
|
|
||
| Implementation (no behavior changes below): | ||
| */ | ||
|
|
||
| function dedupe(arr) { | ||
| // Quick guard: treat null/undefined as empty lists — doesn't throw, just returns [] | ||
| if (arr === null || arr === undefined) { | ||
| return []; | ||
| } | ||
|
|
||
| // If it's not an array, fail fast — makes bugs obvious (helps debugging) | ||
| if (!Array.isArray(arr)) { | ||
| throw new TypeError("Input must be an array"); | ||
| } | ||
|
|
||
| // seen: keep track of values we've already added (O(1) lookup) | ||
| const seen = new Set(); | ||
| // result: build new array, preserve first-seen order | ||
| const result = []; | ||
|
|
||
| // iterate in order; if unseen -> keep it | ||
| for (const item of arr) { | ||
| if (!seen.has(item)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need to check if the set has an item before adding it? |
||
| seen.add(item); | ||
| result.push(item); | ||
| } | ||
| } | ||
|
|
||
| // return the deduped array | ||
| return result; | ||
| } | ||
|
|
||
| module.exports = dedupe; | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,14 @@ | ||
| function findMax(elements) { | ||
| let max = -Infinity; | ||
| for (const value of elements) { | ||
| if (typeof value === "number") { | ||
| max = Math.max(max, value); | ||
| } | ||
| } | ||
| return max; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,11 @@ | ||
| function sum(elements) { | ||
| let total = 0; | ||
| for (let i = 0; i < elements.length; i++) { | ||
| if (typeof elements[i] === "number") { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you know if there are any valid "number" types that you wouldn't be able to add together? |
||
| total += elements[i]; | ||
| } | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| module.exports = sum; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| module.exports = { | ||
| // Treat Sprint-1 as the root for Jest when running from this package | ||
| rootDir: '.', | ||
| // Match tests inside the `fix` and `implement` folders | ||
| // so running `npm test -- fix` will also pick up implement tests like dedupe.test.js | ||
| testMatch: ['<rootDir>/fix/**/*.test.js', '<rootDir>/implement/**/*.test.js'], | ||
| // Ignore node_modules inside Sprint-1 | ||
| testPathIgnorePatterns: ['/node_modules/'], | ||
| modulePathIgnorePatterns: ['/node_modules/'], | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are you checking for here with
isFinite?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To check whether a value is a regular number.