-
-
Notifications
You must be signed in to change notification settings - Fork 278
Sheffield | 26-Jan-ITP | Martha Ogunbiyi| Sprint 1 | Coursework/sprint 1 #1054
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
53784da
605551b
ecc365d
6af6d59
4950064
70627a0
1ded202
e171349
2a8659d
f76457e
8790c12
78292e8
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 |
|---|---|---|
| @@ -1 +1,5 @@ | ||
| function dedupe() {} | ||
| function dedupe(elements) { | ||
| if (elements.length === 0) return []; | ||
| return elements.filter((item, index) => elements.indexOf(item) === index); | ||
| } | ||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,17 @@ | ||
| function findMax(elements) { | ||
| if (!Array.isArray(elements) || elements.length === 0) return -Infinity; | ||
|
|
||
| const numericValues = elements | ||
| .filter( | ||
| (item) => | ||
| (typeof item === "number" && !Number.isNaN(item)) || | ||
| (typeof item === "string" && | ||
| item.trim() !== "" && | ||
| !Number.isNaN(Number(item))) | ||
| ) | ||
| .map(Number); | ||
|
|
||
| return numericValues.length ? Math.max(...numericValues) : -Infinity; | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,28 +16,62 @@ 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", () => { | ||
| const input = [] | ||
| const result = findMax(input) | ||
| expect(result).toBe(-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, its should return that number', () => { | ||
| const input = [5] | ||
| const result = findMax(input) | ||
| expect(result).toBe(5); | ||
| }); | ||
|
|
||
| // 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, it should return largest overall', () => { | ||
| const input = [5, -5, -10, -1, 6, 8, 10] | ||
| const result = findMax(input) | ||
| expect(result).toBe(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 just negative numbers, it should return the closest one to zero', () => { | ||
| const input = [-10, -5, -2, -8]; | ||
| const result = findMax(input); | ||
| expect(result).toBe(-2); | ||
| }) | ||
|
|
||
| // Given an array with decimal numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest decimal number | ||
| // Then it should r 1eturn the largest decimal number | ||
| test("given an array with just decimal numbers, it should return the largest decimal number", () => { | ||
| const input = [1.0, 2.5, 2.2, 5.8]; | ||
| const result = findMax(input); | ||
| expect(result).toBe(5.8); | ||
| }); | ||
|
|
||
| // 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 value, it should return the max and ignore non-numeric value", () => { | ||
| const input = [4, 5, 'arr', 2, []," ", 15, 8, "300"]; | ||
| const result = findMax(input); | ||
| expect(result).toBe(300); | ||
| }); | ||
|
|
||
| // 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 value, it should return the least surprising value given how it behaves for all other inputs", () => { | ||
| const input = [true, {}, "arr", null, [], " ", NaN,]; | ||
| const result = findMax(input); | ||
| expect(result).toBe(-Infinity); | ||
| }); | ||
|
Comment on lines
61
to
+77
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. When a string representing a valid numeric literal (for example, To test if the function can correctly ignore non-numeric values,
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. Thanks for the feedback. I have refactored the implementation to not only check the 'type' , but to also check whether it can become a valid number |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,8 @@ | ||
| function sum(elements) { | ||
| if (!Array.isArray(elements) || elements.length === 0) return 0; | ||
| return elements | ||
| .filter((item) => typeof item === "number" && !Number.isNaN(item)) | ||
| .reduce((acc, n) => acc + n, 0); | ||
| } | ||
|
|
||
| module.exports = sum; |
Uh oh!
There was an error while loading. Please reload this page.