-
-
Notifications
You must be signed in to change notification settings - Fork 211
Sheffield | 25-ITP-Sep | Jak Rhodes-Smith | Sprint 1 | Data Groups #912
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
25bb0ac
7a4e7d3
e501543
08114a5
1d983a0
0105aef
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,8 @@ | ||
| function dedupe() {} | ||
| function dedupe(elements) { | ||
| return elements.reduce( | ||
| (unique, ele) => (unique.includes(ele) ? unique : [...unique, ele]), | ||
| [] | ||
| ); | ||
| } | ||
|
|
||
| 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 numbers = elements | ||
| .filter((val) => typeof val === "number") | ||
| .sort((a, b) => a - b); | ||
|
Comment on lines
+6
to
+8
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.
|
||
|
|
||
| return Math.max(...numbers); | ||
| } | ||
|
|
||
| module.exports = findMax; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,10 @@ | ||
| function sum(elements) { | ||
| return ( | ||
| elements | ||
| .filter((val) => typeof val === "number") | ||
| // Returns initial value(0) for empty arrays | ||
| .reduce((total, current) => total + current, 0) | ||
| ); | ||
| } | ||
|
|
||
| module.exports = sum; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,24 +13,41 @@ const sum = require("./sum.js"); | |
| // Given an empty array | ||
| // When passed to the sum function | ||
| // Then it should return 0 | ||
| test.todo("given an empty array, returns 0") | ||
| test("given an empty array, returns 0", () => { | ||
| expect(sum([])).toBe(0); | ||
| }); | ||
|
|
||
| // Given an array with just one number | ||
| // When passed to the sum function | ||
| // Then it should return that number | ||
| test("given an array with one number, returns that number", () => { | ||
| expect(sum([5])).toBe(5); | ||
| }); | ||
|
|
||
| // Given an array containing negative numbers | ||
| // When passed to the sum function | ||
| // Then it should still return the correct total sum | ||
| test("given an array with negative numbers, returns correct sum", () => { | ||
| expect(sum([10, -5, 20, -3])).toBe(22); | ||
| }); | ||
|
|
||
| // Given an array with decimal/float numbers | ||
| // When passed to the sum function | ||
| // Then it should return the correct total sum | ||
| test("given an array with decimal numbers, returns correct sum", () => { | ||
| expect(sum([10.5, 20.3, 5.2])).toBe(36); | ||
| }); | ||
|
Comment on lines
+37
to
+39
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. Decimal numbers in most programming languages (including JavaScript) are internally represented in floating-point format. So the following could happen expect( 1.2 + 0.6 + 0.005 ).toEqual( 1.805 ); // This will fail
expect( 1.2 + 0.6 + 0.005 ).toEqual( 1.8049999999999997 ); // This will pass
expect( 0.005 + 0.6 + 1.2 ).toEqual( 1.8049999999999997 ); // This will fail
console.log(1.2 + 0.6 + 0.005 == 1.805); // false
console.log(1.2 + 0.6 + 0.005 == 0.005 + 0.6 + 1.2); // falseCan you find a more appropriate way to test a value (that involves decimal-number calculations) for equality? Suggestion: Look up
|
||
|
|
||
| // Given an array containing non-number values | ||
| // When passed to the sum function | ||
| // Then it should ignore the non-numerical values and return the sum of the numerical elements | ||
| test("given an array with non-number values, ignores them and sums numbers", () => { | ||
| expect(sum(["hey", 10, "hi", 60, 10])).toBe(80); | ||
| }); | ||
|
|
||
| // Given an array with only non-number values | ||
| // When passed to the sum 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 0", () => { | ||
| expect(sum(["hey", "hi", "hello"])).toBe(0); | ||
| }); | ||
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.
This test is expected to check also whether the returned array is a copy of the original array --
that is, whether the argument and the returned value of
dedupe(array)are two different arrays.The following test only checks whether both arrays contain the same elements;
it cannot check whether the arrays are actually different objects:
Suggestion: Find out how to test whether two arrays are different arrays in Jest.