-
-
Notifications
You must be signed in to change notification settings - Fork 327
London | 26-ITP-May | Jorvan White | Sprint 1 | Data Groups #1452
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 all commits
2c983b7
6c109e9
d57add3
e3befbb
872ddf0
d98caf6
3566f9b
2633359
11f5bfe
3773eb1
6773b66
58c6017
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 |
|---|---|---|
|
|
@@ -5,10 +5,51 @@ | |
| // Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null) | ||
| // or 'list' has mixed values (the function is expected to sort only numbers). | ||
|
|
||
| function calculateMedian(list) { | ||
| const middleIndex = Math.floor(list.length / 2); | ||
| const median = list.splice(middleIndex, 1)[0]; | ||
| return median; | ||
| function calculateMedian(list) { | ||
| // [ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ] | ||
| if (typeof list === 'string') { | ||
| return null | ||
| } | ||
| if (typeof list === 'number') { | ||
| return null | ||
| } | ||
|
|
||
| if (list === null) { | ||
| return null | ||
| } | ||
| if (typeof list === 'undefined') { | ||
| return null | ||
| } | ||
|
|
||
| if (list === undefined) { | ||
| return null | ||
| } | ||
| if (typeof list === 'object' && !Array.isArray(list)) { | ||
| return null | ||
| } | ||
| if (Array.isArray(list) && list.length === 0){ | ||
| return null | ||
| } | ||
| list = list.filter(element => typeof element === 'number'); | ||
| if (list.length === 0) { | ||
| return null | ||
| } | ||
| list.sort((a, b) => a - b); | ||
|
|
||
| if (list.length % 2 === 0){ | ||
| const middleIndexR = Math.floor(list.length / 2); | ||
| const middleIndexL = middleIndexR - 1 | ||
| const evenMedian = (list[middleIndexL] + list[middleIndexR]) / 2; | ||
|
|
||
| return evenMedian | ||
| } else { | ||
| const middleIndex = Math.floor(list.length / 2); | ||
| const median = list[middleIndex]; | ||
| return median; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| calculateMedian(['apple']) | ||
|
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. please move any test case into |
||
|
|
||
| module.exports = calculateMedian; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,5 @@ | ||
| function dedupe() {} | ||
| function dedupe(list) { | ||
| return [...new Set(list)]; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,8 @@ | ||
| function findMax(elements) { | ||
| elements = elements.filter(element => typeof element === 'number'); | ||
| if (elements.length === 0){ | ||
| return -Infinity; | ||
| } | ||
| return Math.max(...elements); | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,28 +16,49 @@ const findMax = require("./max.js"); | |
| // When passed to the max function | ||
| // Then it should return -Infinity | ||
| // Delete this test.todo and replace it with a test. | ||
| test.todo("given an empty array, returns -Infinity"); | ||
| test("given an empty array, returns -Infinity", () => { | ||
| expect(findMax([])).toEqual(-Infinity); | ||
| }); | ||
|
|
||
| // Given an array with one number | ||
| // When passed to the max function | ||
| // Then it should return that number | ||
| test("given an array with one number, returns the same number", () => { | ||
| expect(findMax([4])).toEqual(4); | ||
| }); | ||
|
|
||
| // Given an array with both positive and negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest number overall | ||
| test("given an array with both positive and negative numbers, returns the largest number", () => { | ||
| expect(findMax([10, -10])).toEqual(10); | ||
| }); | ||
|
|
||
| // Given an array with just negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the closest one to zero | ||
| test("given an array with negative numbers, returns the number closest to zero", () => { | ||
| expect(findMax([-3 , -15, -4, -11])).toEqual(-3); | ||
| }); | ||
|
|
||
|
|
||
| // Given an array with decimal numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest decimal number | ||
| test("given an array with negative numbers, returns the number closest to zero", () => { | ||
| expect(findMax([-3 , -15, -4, -11])).toEqual(-3); | ||
| }); | ||
|
Comment on lines
45
to
+50
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. This test case is the same as above. It should be testing decimal numbers (e.g. all positive, all negative and combined). |
||
|
|
||
| // Given an array with non-number values | ||
| // When passed to the max function | ||
| // Then it should return the max and ignore non-numeric values | ||
| test("given an array with non-number values, ignore the non numeric values and return the max", () => { | ||
| expect(findMax(['car', 'house', 1, 12, 'fish'])).toEqual(12); | ||
| }); | ||
|
|
||
| // Given an array with only non-number values | ||
| // When passed to the max function | ||
| // Then it should return the least surprising value given how it behaves for all other inputs | ||
| test("given an array with only non-number values, returns -Infinity", () => { | ||
| expect(findMax(['car', 'house', 'fish', 'car'])).toEqual(-Infinity); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,17 @@ | ||
| function sum(elements) { | ||
| elements = elements.filter(element => typeof element === 'number'); | ||
| if (elements.length === 0) { | ||
| return null;} | ||
|
Comment on lines
+3
to
+4
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. According to the test case, given an empty array, it should return |
||
|
|
||
| let total = 0; | ||
|
|
||
| for (let element of elements) { | ||
| total += element; | ||
| // += means to add to the current value and assign as result | ||
| // elements are individual items inside a collection (not just string) | ||
| } | ||
|
|
||
| return total; | ||
| } | ||
|
|
||
| module.exports = sum; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,13 @@ | ||
| // 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 (const element of list) { | ||
| if (element === target) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
|
|
||
| module.exports = includes; |
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.
These cases are all return null and can be simplified into one.