-
-
Notifications
You must be signed in to change notification settings - Fork 220
London | ITP-MAY-25 | Surafel Workneh| Module: Data Groups | Sprint-1 #681
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
Changes from all commits
3b68660
552d6b1
826e24b
b2b889c
a226f7d
038d537
d2749fc
7c3a42d
82243a3
ef76a1f
c20e286
0ba1d5f
d7b2aff
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,14 +1,21 @@ | ||
| // Fix this implementation | ||
| // Start by running the tests for this function | ||
| // If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory | ||
| function calculateMedian(list) { | ||
| // Check if input is a valid array | ||
| if (!Array.isArray(list)) return null; | ||
|
|
||
| // Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null) | ||
| // or 'list' has mixed values (the function is expected to sort only numbers). | ||
| // Filter out only numeric values | ||
| const nums = list.filter(val => typeof val === 'number' && !isNaN(val)); | ||
| if (nums.length === 0) return null; | ||
|
|
||
| function calculateMedian(list) { | ||
| const middleIndex = Math.floor(list.length / 2); | ||
| const median = list.splice(middleIndex, 1)[0]; | ||
| return median; | ||
| // Clone and sort the array numerically | ||
| const sorted = [...nums].sort((a, b) => a - b); | ||
| const mid = Math.floor(sorted.length / 2); | ||
|
|
||
| // Return median based on even or odd length | ||
| if (sorted.length % 2 !== 0) { | ||
| return sorted[mid]; | ||
| } else { | ||
| return (sorted[mid - 1] + sorted[mid]) / 2; | ||
| } | ||
| } | ||
|
|
||
| module.exports = calculateMedian; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,17 @@ | ||
| function dedupe() {} | ||
| function dedupe(arr) { | ||
| if (!Array.isArray(arr)) return []; | ||
|
|
||
| const seen = new Set(); | ||
| const result = []; | ||
|
|
||
| for (const item of arr) { | ||
| if (!seen.has(item)) { | ||
| seen.add(item); | ||
| result.push(item); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
|
Comment on lines
+1
to
+16
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. Very good. Using a |
||
| module.exports = dedupe; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,11 @@ | ||
|
|
||
|
|
||
| function findMax(elements) { | ||
| if (!Array.isArray(elements)) return -Infinity; | ||
|
|
||
| const nums = elements.filter(val => typeof val === 'number' && !isNaN(val)); | ||
|
|
||
| return nums.length > 0 ? Math.max(...nums) : -Infinity; | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,9 @@ | ||
| function sum(elements) { | ||
| if (!Array.isArray(elements)) return 0; | ||
|
|
||
| return elements.reduce((acc, val) => { | ||
| return typeof val === "number" && !isNaN(val) ? acc + val : acc; | ||
| }, 0); | ||
| } | ||
|
Comment on lines
1
to
7
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. Very good. However using |
||
|
|
||
| module.exports = sum; | ||
| module.exports = sum; | ||
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.
Overall this is excellent. But is it necessary to clone the array here? Could we just sort it?