-
-
Notifications
You must be signed in to change notification settings - Fork 193
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
base: main
Are you sure you want to change the base?
Changes from all commits
123b714
9bd3912
6e23d98
1026829
550590f
2cc519c
0799426
821e75a
5edb209
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? |
||
| 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; | ||
|
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. This is a very robust solution, but too complex for the sake of the learning purposes, could we assume that the max float decimal places are 2 ? as we see in tests ? |
||
|
|
||
| const total = numbers.reduce((acc, el) => acc + el, 0); | ||
|
|
||
| return Math.round((total + Number.EPSILON) * decMultiplier) / decMultiplier; | ||
|
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. Therefore as a consequence of the comment above, we will not need Epsilon here as MathRound will do its work as expected. |
||
| } | ||
|
|
||
| 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.