-
-
Notifications
You must be signed in to change notification settings - Fork 249
NOT-A-STUDENT-PR: Update Solution and tests for Sprint 1 'fix median' task #390
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
e2d0aff
7e1c7e3
a6fd706
504c533
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 |
|---|---|---|
|
|
@@ -7,20 +7,42 @@ | |
| const calculateMedian = require("./median.js"); | ||
|
|
||
| describe("calculateMedian", () => { | ||
| it("returns the median for odd length array", () => { | ||
| expect(calculateMedian([1, 2, 3])).toEqual(2); | ||
| expect(calculateMedian([1, 2, 3, 4, 5])).toEqual(3); | ||
| }); | ||
| [ | ||
| { 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)) | ||
| ); | ||
|
|
||
| it("returns the average of middle values for even length array", () => { | ||
| expect(calculateMedian([1, 2, 3, 4])).toEqual(2.5); | ||
| expect(calculateMedian([1, 2, 3, 4, 5, 6])).toEqual(3.5); | ||
| }); | ||
| [ | ||
| { 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 }, | ||
| ].forEach(({ input, expected }) => | ||
| it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) | ||
| ); | ||
|
|
||
| it("doesn't modify the input", () => { | ||
| it("doesn't modify the input array [1, 2, 3]", () => { | ||
| const list = [1, 2, 3]; | ||
| calculateMedian(list); | ||
|
|
||
| expect(list).toEqual([1, 2, 3]); | ||
| }); | ||
| }); | ||
|
|
||
| [ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val => | ||
|
Member
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. We should add these tests to the
Contributor
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. Absolutely, the |
||
| 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)) | ||
| ); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.