-
-
Notifications
You must be signed in to change notification settings - Fork 211
London|ITP-September-2025|Mariia Serhiienko|Sprint 1|Implement #896
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 9 commits
123b714
9bd3912
6e23d98
1026829
550590f
2cc519c
0799426
821e75a
5edb209
9df7c23
e027a30
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 |
|---|---|---|
|
|
@@ -6,9 +6,26 @@ | |
| // or 'list' has mixed values (the function is expected to sort only numbers). | ||
|
|
||
| function calculateMedian(list) { | ||
| const middleIndex = Math.floor(list.length / 2); | ||
| const median = list.splice(middleIndex, 1)[0]; | ||
| return median; | ||
| if (!Array.isArray(list) || list.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| const filteredNumericArr = list.filter( | ||
| (el) => typeof el === "number" && Number.isFinite(el) | ||
| ); | ||
|
|
||
| if (filteredNumericArr.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| const sortedArr = [...filteredNumericArr].sort((a, b) => a - b); | ||
| const middleIndex = Math.floor(sortedArr.length / 2); | ||
|
|
||
| if (sortedArr.length % 2 !== 0) { | ||
|
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. Why do we need this last logic from 24 to 28 lines?
Author
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. Because when the list has an even number of numbers, there isn’t just one middle value, but there are two. In that situation, the median is defined as the average of those two middle numbers. |
||
| return sortedArr[middleIndex]; | ||
| } else { | ||
| return (sortedArr[middleIndex - 1] + sortedArr[middleIndex]) / 2; | ||
| } | ||
| } | ||
|
|
||
| module.exports = calculateMedian; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,15 @@ | ||
| function dedupe() {} | ||
| function dedupe(arr) { | ||
| if (arr.length === 0) return []; | ||
|
|
||
| const noDupsArr = []; | ||
| const seenItems = new Set(); | ||
|
|
||
| for (const el of arr) { | ||
| if (!seenItems.has(el)) { | ||
| noDupsArr.push(el); | ||
| seenItems.add(el); | ||
| } | ||
| } | ||
| return noDupsArr; | ||
| } | ||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,16 @@ | ||
| function findMax(elements) { | ||
| if (elements.length === 0) { | ||
| return -Infinity; | ||
| } else if (elements.length === 1 && typeof elements[0] === "number") { | ||
| return elements[0]; | ||
| } | ||
| const maxEl = Math.max( | ||
| ...elements.filter((el) => typeof el === "number" && !isNaN(el)) | ||
| ); | ||
|
|
||
| if (elements.length > 1) { | ||
| return maxEl; | ||
| } | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,19 @@ | ||
| function sum(elements) { | ||
| const numbers = elements.filter( | ||
| (item) => typeof item === "number" && !isNaN(item) | ||
| ); | ||
|
|
||
| if (numbers.length === 0) return 0; | ||
|
|
||
| const maxDecimalPlaces = numbers.reduce( | ||
| (currMax, el) => Math.max(currMax, (String(el).split(".") || "").length), | ||
| 0 | ||
| ); | ||
| const decMultiplier = 10 ** maxDecimalPlaces; | ||
|
||
|
|
||
| const total = numbers.reduce((acc, el) => acc + el, 0); | ||
|
|
||
| return Math.round((total + Number.EPSILON) * decMultiplier) / decMultiplier; | ||
|
||
| } | ||
|
|
||
| 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.
According to
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite
isFinite already checks if the element is a number ( typeof el === "number" ).
I like the usage of the method tho.
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.
Thank you for pointing this ! I definetely missed this, so now fixed.