-
-
Notifications
You must be signed in to change notification settings - Fork 327
London | 26-ITP-Sep| Mahdi Rafiei | Sprint 1 | Sprint1 exercises #1465
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
Open
mrafeie
wants to merge
13
commits into
CodeYourFuture:main
Choose a base branch
from
mrafeie:sprint1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7fae3a8
Refactor calculateMedian to validate input and sort numbers
mrafeie 50ea4e6
Implement tests for calculateMedian function
mrafeie 1c3f9f1
Implement dedupe function to remove duplicates
mrafeie 3c5209c
Update dedupe.test.js
mrafeie 5b5d17c
Enhance findMax function to handle non-numeric values
mrafeie 42df8a4
Update tests for findMax function in max.test.js
mrafeie 2bade65
Enhance sum function to handle non-number elements
mrafeie 0f8092e
Update test cases for sum function
mrafeie 51c2c9d
Add describeMedian function implementation
mrafeie ddb583a
Replace test.todo with actual test for describeMedian
mrafeie 9c3e821
Implement calculateMean function with validation
mrafeie 8f6a734
Implement tests for calculateMean function
mrafeie 2cc2ab5
Refactor includes function to use for...of loop
mrafeie 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,50 @@ | ||
| // Fix this implementation | ||
| // Start by running the tests for this function | ||
| // If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory | ||
| // median.test.js | ||
|
|
||
| // Hint: Please consider scenarios when 'list' isn't an array, is empty, | ||
| // or contains values that aren't numbers (the function is expected to throw - see the tests). | ||
| // Someone has implemented calculateMedian but it isn't | ||
| // passing all the tests... | ||
| // Fix the implementation of calculateMedian so it passes all tests | ||
|
|
||
| function calculateMedian(list) { | ||
| const middleIndex = Math.floor(list.length / 2); | ||
| const median = list.splice(middleIndex, 1)[0]; | ||
| return median; | ||
| } | ||
| const calculateMedian = require("./median.js"); | ||
|
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. Where is median implemented? |
||
|
|
||
| module.exports = calculateMedian; | ||
| describe("calculateMedian", () => { | ||
| [ | ||
| { input: [1, 2, 3], expected: 2 }, | ||
| { input: [1, 2, 3, 4, 5], expected: 3 }, | ||
| { input: [1, 2, 3, 4], expected: 2.5 }, | ||
| { input: [1, 2, 3, 4, 5, 6], expected: 3.5 }, | ||
| ].forEach(({ input, expected }) => | ||
| it(`returns the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) | ||
| ); | ||
|
|
||
| [ | ||
| { input: [3, 1, 2], expected: 2 }, | ||
| { input: [5, 1, 3, 4, 2], expected: 3 }, | ||
| { input: [4, 2, 1, 3], expected: 2.5 }, | ||
| { input: [6, 1, 5, 3, 2, 4], expected: 3.5 }, | ||
| { input: [110, 20, 0], expected: 20 }, | ||
| { input: [6, -2, 2, 12, 14], expected: 6 }, | ||
| ].forEach(({ input, expected }) => | ||
| it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) | ||
| ); | ||
|
|
||
| it("doesn't modify the input array [3, 1, 2]", () => { | ||
| const list = [3, 1, 2]; | ||
| calculateMedian(list); | ||
| expect(list).toEqual([3, 1, 2]); | ||
| }); | ||
|
|
||
| [ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val => | ||
| it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null)) | ||
| ); | ||
|
|
||
| [ | ||
| { input: [1, 2, "3", null, undefined, 4], expected: 2 }, | ||
| { input: ["apple", 1, 2, 3, "banana", 4], expected: 2.5 }, | ||
| { input: [1, "2", 3, "4", 5], expected: 3 }, | ||
| { input: [1, "apple", 2, null, 3, undefined, 4], expected: 2.5 }, | ||
| { input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 }, | ||
| { input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 }, | ||
| ].forEach(({ input, expected }) => | ||
| it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) | ||
| ); | ||
| }); | ||
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,5 @@ | ||
| function dedupe() {} | ||
| function dedupe(arr) { | ||
| return [...new Set(arr)]; | ||
| } | ||
|
|
||
| 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
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,17 @@ | ||
| function findMax(elements) { | ||
| const numbers = elements.filter((item) => typeof item === "number"); | ||
| if (numbers.length === 0) { | ||
| return -Infinity; | ||
| } | ||
| let max = -Infinity; | ||
|
|
||
| for (const num of numbers) { | ||
| if (num > max) { | ||
| max = num; | ||
| } | ||
| } | ||
|
|
||
| return max; | ||
| } | ||
|
|
||
| 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,3 +1,17 @@ | ||
| function calculateMean(list) {} | ||
| function calculateMean(list) { | ||
| if (!Array.isArray(list)) { | ||
| throw new Error("calculateMean requires an array of numbers"); | ||
| } | ||
| if (list.length === 0) { | ||
| throw new Error("calculateMean requires a non-empty array"); | ||
| } | ||
| for (const item of list) { | ||
| if (typeof item !== "number") { | ||
| throw new Error("calculateMean requires an array of numbers"); | ||
| } | ||
| } | ||
| const total = list.reduce((sum, number) => sum + number, 0); | ||
| return total / list.length; | ||
| } | ||
|
|
||
| module.exports = calculateMean; |
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,9 @@ | ||
| function sum(elements) { | ||
| const numbers = elements.filter((item) => typeof item === "number"); | ||
| if (numbers.length === 0) { | ||
| return 0; | ||
| } | ||
| return numbers.reduce((total, num) => total + num, 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. Good use of the reduce function here |
||
| } | ||
|
|
||
| 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
Oops, something went wrong.
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.
Did you run your tests before submitting the PR? I get some errors on the median specifically.