Repository navigation
London | 26-ITP-Sep | Bartosz Kawiak | Sprint 1 | Data Groups #1478
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 8 commits
a5d9c95
5e63389
8e2d553
552c695
0cdf000
98d4d64
8562710
df1f5cb
8d0ec88
f5ab95b
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,14 @@ | ||
| function dedupe() {} | ||
| function dedupe(arr) { | ||
| if (arr.length === 0) { | ||
| return arr; | ||
| } | ||
| const newArr = []; | ||
| for (const item of arr) { | ||
| if (!newArr.includes(item)) { | ||
| newArr.push(item); | ||
| } | ||
| } | ||
| return newArr; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,19 @@ | ||
| function findMax(elements) { | ||
| if (!Array.isArray(elements)) { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
| if (elements.includes(NaN)) { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
| if (elements.length === 0) { | ||
| return -Infinity; | ||
| } | ||
|
|
||
| if (elements.length === 1) { | ||
| return elements[0]; | ||
| } | ||
| const sortedList = elements.toSorted((a, b) => a - b); | ||
| return sortedList.at(-1); | ||
|
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. Your code works, but there is a more efficient way to find the extreme value in an array without sorting it, as sorting can be a relatively costly operation. This approach uses a simple loop and works with arrays of any size. Since finding the maximum or minimum value in an array is a common programming task, could you research how this approach works and then implement it in your code?
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. I believe it's sorted now. Thank you for the feedback CJ. |
||
| } | ||
|
|
||
| module.exports = findMax; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,26 @@ | ||
| function calculateMean(list) {} | ||
| function calculateMean(list) { | ||
| if (!Array.isArray(list)) { | ||
| throw new Error("calculateMean requires an array of numbers"); | ||
| } | ||
| for (const item of list) { | ||
| if (typeof item !== "number") { | ||
| throw new Error("calculateMean requires an array of numbers"); | ||
| } | ||
| } | ||
| if (list.length === 0) { | ||
| throw new Error("calculateMean requires a non-empty array"); | ||
| } | ||
| if (list.length === 1) { | ||
| return list[0]; | ||
| } | ||
|
|
||
| let result = 0; | ||
| let totalItem = 0; | ||
| for (const item of list) { | ||
| totalItem += item; | ||
| result = totalItem / list.length; | ||
|
cjyuan marked this conversation as resolved.
Resolved
|
||
| } | ||
| return result; | ||
| } | ||
|
|
||
| module.exports = calculateMean; | ||
|
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. I forgot to raise this question in my previous review: What should |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,21 @@ | ||
| function sum(elements) { | ||
| if (!Array.isArray(elements)) { | ||
| throw new Error("sum requires an array of numbers"); | ||
| } | ||
| if (elements.includes(NaN)) { | ||
| throw new Error("sum requires an array of numbers"); | ||
| } | ||
| if (elements.length === 0) { | ||
| return 0; | ||
| } | ||
| if (elements.length === 1) { | ||
| return elements[0]; | ||
| } | ||
| let total = 0; | ||
| for (const item of elements) { | ||
| total += item; | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| module.exports = sum; |
Uh oh!
There was an error while loading. Please reload this page.