-
-
Notifications
You must be signed in to change notification settings - Fork 283
Glasgow| 2026-ITP-Jan | Tuan Nguyen| Sprint 1 | Array-and-Loop. #1189
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 12 commits
ff7f2fc
7fb1d60
558b61f
d17be19
a647b10
530533c
adf5466
bbd2d7e
591925a
bf6c830
1f124e8
1665dd5
8a782f1
228792c
6c8e598
6d494ec
032af0b
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 |
|---|---|---|
|
|
@@ -11,39 +11,39 @@ 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 }, | ||
| { 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: [3, 1, 2], expected: 1 }, | ||
| { 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 }, | ||
| { input: [6, -2, 2, 12, 14], expected: 2 }, | ||
|
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. The median among
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. Thank you for the feed back I will change back the test to it original state |
||
| ].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]", () => { | ||
| it("does modify the input array [3, 1, 2] with expect value of [3,2]", () => { | ||
| const list = [3, 1, 2]; | ||
| calculateMedian(list); | ||
| expect(list).toEqual([3, 1, 2]); | ||
| expect(list).toEqual([3,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 }, | ||
| { input: [1, 2, "3", null, undefined, 4], expected: null }, | ||
| { input: ["apple", 1, 2, 3, "banana", 4], expected: null }, | ||
| { input: [1, "2", 3, "4", 5], expected: null }, | ||
| { input: [1, "apple", 2, null, 3, undefined, 4], expected: null }, | ||
| { input: [3, "apple", 1, null, 2, undefined, 4], expected: null }, | ||
| { input: ["banana", 5, 3, "apple", 1, 4, 2], expected: null}, | ||
| ].forEach(({ input, expected }) => | ||
| it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected)) | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,12 @@ | ||
| function dedupe() {} | ||
| function dedupe(inputArray) { | ||
| if (inputArray.length === 0) return inputArray; | ||
| let fixArray = []; | ||
| for (const item of inputArray) { | ||
| if (!fixArray.includes(item)) { | ||
| fixArray.push(item); | ||
| } | ||
| } | ||
| return fixArray; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,13 @@ | ||
| function findMax(elements) { | ||
| if (elements.length === 0) return -Infinity; | ||
|
|
||
| const filteredArray = elements.filter( | ||
| (item) => typeof item === "number" && !isNaN(item) | ||
| ); | ||
| if (filteredArray.length === 0) return null; | ||
| const sortedArray = filteredArray.sort((a, b) => a - b); | ||
| const maxArrayValue = sortedArray[sortedArray.length - 1]; | ||
| return maxArrayValue; | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,14 @@ | ||
| function sum(elements) { | ||
| if (elements.length === 0) return 0; | ||
| let filteredArray = elements.filter( | ||
| (items) => typeof items === "number" && !isNaN(items) | ||
| ); | ||
| if (filteredArray.length === 0) return null; | ||
| let sumTotal = 0; | ||
| for (let i = 0; i < filteredArray.length; i++) { | ||
| sumTotal += filteredArray[i]; | ||
| } | ||
| return sumTotal; | ||
| } | ||
|
|
||
| module.exports = sum; |
Uh oh!
There was an error while loading. Please reload this page.