-
-
Notifications
You must be signed in to change notification settings - Fork 272
Cape Town | 26-ITP-Jan | Pretty Taruvinga | Sprint 1 | Data Groups #1104
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
53fa90f
86132f9
33ec99a
4bf6702
5db3aac
fc16e68
0e7b9da
d4e734a
459eb79
0a5f5cc
d55358c
5514777
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,7 @@ | ||
| function dedupe() {} | ||
| function dedupe(list) { | ||
| if (!Array.isArray(list)) { | ||
| return null; | ||
| } | ||
| return list.filter((value, index, self) => self.indexOf(value) === index); | ||
| } | ||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,21 @@ | ||
| function findMax(elements) { | ||
| if (!Array.isArray(elements) || elements.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| let max = -Infinity; | ||
|
|
||
| for (let i = 0; i < elements.length; i++) { | ||
| if (typeof elements[i] === "number" && !isNaN(elements[i])) { | ||
| if (elements[i] > max) { | ||
| max = elements[i]; | ||
| } | ||
| } | ||
| } | ||
| if (max === -Infinity) { | ||
| return null; | ||
| } | ||
|
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 spec in
|
||
| return max; | ||
| } | ||
|
|
||
| module.exports = findMax; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,28 +16,48 @@ 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, when passed to the max function, then it should return null", () => { | ||
| expect(findMax([])).toBeNull(); | ||
| }); | ||
|
|
||
| // 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, when passed to the max function, then it should return that number", () => { | ||
| expect(findMax([42])).toBe(42); | ||
| }); | ||
|
|
||
| // 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, when passed to the max function, then it should return the largest number overall", () => { | ||
| expect(findMax([-10, -5, 0, 5, 10])).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, when passed to the max function, then it should return the closest one to zero", () => { | ||
| expect(findMax([-10, -5, -1])).toBe(-1); | ||
| }); | ||
|
|
||
| // 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 decimal numbers, when passed to the max function, then it should return the largest decimal number", () => { | ||
| expect(findMax([1.5, 2.3, 0.8, 3.1])).toBe(3.1); | ||
| }); | ||
|
|
||
| // 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, when passed to the max function, then it should return the max and ignore non-numeric values", () => { | ||
| expect(findMax(["hey", 10, "hi", 60, 10])).toBe(60); | ||
| }); | ||
|
Comment on lines
+54
to
+56
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, |
||
|
|
||
| // 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, when passed to the max function, then it should return null", () => { | ||
| expect(findMax(["hey", "hi", "hello"])).toBeNull(); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,14 @@ | ||
| function sum(elements) { | ||
| if (!Array.isArray(elements)) { | ||
| return null; | ||
| } | ||
| let total = 0; | ||
| for (let i = 0; i < elements.length; i++) { | ||
| if (typeof elements[i] === "number" && !isNaN(elements[i])) { | ||
| total += elements[i]; | ||
| } | ||
| } | ||
| return Number(total.toFixed(10)); | ||
|
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. Why reduce the precision? |
||
| } | ||
|
|
||
| module.exports = sum; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,24 +13,50 @@ 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, when passed to the sum function, then it should return 0", () => { | ||
| expect(sum([])).toBe(0); | ||
| }); | ||
|
|
||
| // Given a non-array value | ||
| // When passed to the sum function | ||
| // Then it should return null | ||
| test("Given a non-array value, when passed to the sum function, then it should return null", () => { | ||
| expect(sum("not an array")).toBeNull(); | ||
| expect(sum(123)).toBeNull(); | ||
| expect(sum({})).toBeNull(); | ||
| }); | ||
|
|
||
| // Given an array with just one number | ||
| // When passed to the sum function | ||
| // Then it should return that number | ||
| test("Given an array with just one number, when passed to the sum function, then it should return that number", () => { | ||
| expect(sum([42])).toBe(42); | ||
| }); | ||
|
|
||
| // 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 containing negative numbers, when passed to the sum function, then it should still return the correct total sum", () => { | ||
| expect(sum([-10, -20, -30])).toBe(-60); | ||
| }); | ||
|
|
||
| // 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/float numbers, when passed to the sum function, then it should return the correct total sum", () => { | ||
| expect(sum([1.5, 2.3, 0.8, 3.1])).toBe(7.7); | ||
| }); | ||
|
Comment on lines
+46
to
+48
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 JS) are internally represented in "floating point number" format. Floating point arithmetic is not exact. For example, the result of So the following could happen expect( 1.2 + 0.6 + 0.005 ).toEqual( 1.805 ); // This fail
expect( 1.2 + 0.6 + 0.005 ).toEqual( 1.8049999999999997 ); // This pass
expect( 0.005 + 0.6 + 1.2 ).toEqual( 1.8049999999999997 ); // This 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); // falseInstead of reducing the precision of the calculation in the 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 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", () => { | ||
| 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, when passed to the sum function, then it should return 0", () => { | ||
| expect(sum(["hey", "hi", "hello"])).toBe(0); | ||
| }); | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Your function implementation is correct. However, this test could be improved to better ensure
that any future changes continue to align with the expected behavior described on line 34:
This test should fail if the function returns the original array (instead of a copy of the original array).
The current test checks only if both the original array and the returned array contain identical elements.
In order to validate the returned array is a different array, we need an additional check.
Can you find out what this additional check is?