generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 212
West Midlands | 25-ITP-Sept | Georgina Rogers | Sprint 1 | Sprint 1 Coursework #841
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a70c42b
Fix: update calculateMedian implementation to correctly handle median…
zilinskyte ae016ef
write and test findMax function to handle a variety of cases
zilinskyte 49e91dd
sum function written and passes all tests
zilinskyte 8323142
dedupe function written and passing all tests
zilinskyte c6076ed
Refactored function to include a for...of loop
zilinskyte 45d4be7
Refactor calculateMedian to eliminate unnecessary array cloning befor…
zilinskyte 8aae3f8
Refactor findMax to eliminate unnecessary array filtering
zilinskyte 214e442
Refactor sum function to use Number.isFinite for better number valida…
zilinskyte eb7b89f
Add test to ensure dedupe returns a new array reference
zilinskyte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,14 @@ | ||
| function dedupe() {} | ||
| function dedupe(arr) { | ||
| if (!Array.isArray(arr) || arr.length === 0) { | ||
| return []; | ||
| } | ||
| const result = []; // array to store unique elements | ||
| for (let element of arr) { | ||
| if (!result.includes(element)) { | ||
| // check if element is not already in result | ||
| result.push(element); // add unique element to result array | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| module.exports = dedupe; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,18 @@ | ||
| function findMax(elements) { | ||
| if (!Array.isArray(elements) || elements.length === 0) { | ||
| return -Infinity; | ||
| } | ||
| let max = -Infinity; | ||
|
|
||
| for (const x of elements) { | ||
| if (typeof x === "number" && Number.isFinite(x)) { | ||
| if (x > max) max = x; | ||
| } | ||
| } | ||
|
|
||
| return max; | ||
| // removed .filter as this creates a new array so this avoids unnecessary clones | ||
|
|
||
|
Comment on lines
+2
to
+15
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. Indentation is off. |
||
| } | ||
|
|
||
| module.exports = findMax; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,14 @@ | ||
| function sum(elements) { | ||
| } | ||
| function sum(arr) { | ||
| let total = 0; // variable keeps track of the sum | ||
|
|
||
| for (let element of arr) { | ||
| // iterate through each element in the array | ||
| if (Number.isFinite(element)) { | ||
| // check if the element is a number, without counting NaN or Infinity | ||
| total += element; // add the number to the total sum | ||
| } | ||
| } | ||
|
|
||
| return total; // return the final sum | ||
| } | ||
| module.exports = sum; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,14 @@ | ||
| // Refactor the implementation of includes to use a for...of loop | ||
|
|
||
| function includes(list, target) { | ||
| for (let index = 0; index < list.length; index++) { | ||
| const element = list[index]; | ||
| for (let element of list) { | ||
| // iterate through each element in the list | ||
| if (element === target) { | ||
| return true; | ||
| // check if the current element matches the target | ||
| return true; // return true if a match is found and exit the function | ||
| } | ||
| } | ||
| return false; | ||
| return false; // return false if no match is found after checking all elements | ||
| } | ||
|
|
||
| module.exports = includes; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
.slice()also creates a new array.