-
-
Notifications
You must be signed in to change notification settings - Fork 283
GLASGOW | Jan-26-ITP | Prakash Dcosta | Sprint 1 | Data Groups #1005
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
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,29 @@ | |
| // 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; | ||
| // It must be an array | ||
| if (!Array.isArray(list)) { | ||
| return null; | ||
| } | ||
| // Keep only numeric values | ||
| const numbers = list.filter( | ||
| (value) => typeof value === "number" && !isNaN(value) | ||
| ); | ||
|
|
||
| // If there are no numbers existing then return null | ||
| if (numbers.length === 0) return null; | ||
|
|
||
| // Sort numbers without modifying original list | ||
| const sorted = [...numbers].sort((a, b) => a - b); | ||
|
Contributor
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. Is it necessary to make a copy of
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. No, it is not necessary to make a copy of numbers before sorting, however, it is a good practice.
Contributor
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. Can you elaborate how it is a good practice in this scenario?
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. In this scenario it is not needed. I just tried to make it cleaner by removing non numeric values if any. Do you want me to remove it?
Contributor
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. "... to make it cleaner by removing non numeric values" -- You have already removed non numeric values using You can keep the code unchanged. I am more interested in knowing why you think "making a copy of
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. Ok. I got your point. According to me, making a copy before sorting is a good practice for the following reasons:
Contributor
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.
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. • Yes by file I meant array.
Contributor
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. "immutability" do has its advantage. The reasons you gave do not quite fit the Thought you might be interested in this ChatGPT explaination:
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. Ok. I will look at it in detail. Thank you |
||
| const middle = Math.floor(sorted.length / 2); | ||
|
|
||
| // For even length | ||
| if (sorted.length % 2 === 0) { | ||
| return (sorted[middle - 1] + sorted[middle]) / 2; | ||
| } | ||
|
|
||
| // For odd length | ||
| return sorted[middle]; | ||
| } | ||
|
|
||
| module.exports = calculateMedian; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| function dedupe() {} | ||
| function dedupe(arr) { | ||
| return [...new Set(arr)]; | ||
| } | ||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,13 @@ | ||
| function findMax(elements) { | ||
| function findMax(arr) { | ||
| if (!Array.isArray(arr)) return -Infinity; | ||
|
|
||
| let max = -Infinity; | ||
| for (let i = 0; i < arr.length; i++) { | ||
| if (typeof arr[i] === "number" && arr[i] > max) { | ||
| max = arr[i]; | ||
| } | ||
| } | ||
| return max; | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,14 @@ | ||
| function sum(elements) { | ||
| function sum(arr) { | ||
| let total = 0; | ||
| for (let i = 0; i < arr.length; i++) { | ||
| if (typeof arr[i] === "number" && !isNaN(arr[i])) { | ||
| total += arr[i]; | ||
| } | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| let arr = [Infinity, -Infinity]; | ||
| console.log(sum(arr)); | ||
|
|
||
| module.exports = sum; |
Uh oh!
There was an error while loading. Please reload this page.