-
-
Notifications
You must be signed in to change notification settings - Fork 319
Manchester | 26-ITP-May | Samreen Amjad | Sprint 1 | Exercises #1291
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
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,11 @@ | ||
| function dedupe() {} | ||
| function dedupe(array) { | ||
| const unique = []; | ||
| array.forEach((item) => { | ||
| if (!unique.includes(item)) { | ||
| unique.push(item); | ||
| } | ||
| }); | ||
| return unique; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,9 @@ | ||
| function findMax(elements) { | ||
| function findMax(list) { | ||
| const numbers = list.filter((n) => Number.isFinite(n)); | ||
| if (numbers.length == 0) { | ||
| return -Infinity; | ||
| } else { | ||
| return Math.max(...numbers); | ||
| } | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,10 @@ | ||
| function sum(elements) { | ||
| function sum(list) { | ||
| const numbers = list.filter((n) => Number.isFinite(n)); | ||
| let total = 0; | ||
| for (n of numbers) { | ||
|
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. Should also declare |
||
| total += n; | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| module.exports = sum; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,27 +10,56 @@ const sum = require("./sum.js"); | |
|
|
||
| // Acceptance Criteria: | ||
|
|
||
| // Given an empty array | ||
| // When passed to the sum function | ||
| // Then it should return 0 | ||
| test.todo("given an empty array, returns 0") | ||
|
|
||
| // Given an array with just one number | ||
| // When passed to the sum function | ||
| // Then it should return that number | ||
|
|
||
| // Given an array containing negative numbers | ||
| // When passed to the sum function | ||
| // Then it should still return the correct total sum | ||
|
|
||
| // Given an array with decimal/float numbers | ||
| // When passed to the sum function | ||
| // Then it should return the correct total sum | ||
|
|
||
| // 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 | ||
|
|
||
| // 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 | ||
| describe("sum", () => { | ||
| // Given an empty array | ||
| // When passed to the sum function | ||
| // Then it should return 0 | ||
| it("empty array returns 0", () => { | ||
| const list = []; | ||
| expect(sum(list)).toEqual(0); | ||
| }); | ||
|
|
||
| // Given an array with just one number | ||
| // When passed to the sum function | ||
| // Then it should return that number | ||
| it("array with one number returns number", () => { | ||
| const list = [1]; | ||
| expect(sum(list)).toEqual(1); | ||
| }); | ||
|
|
||
| // Given an array containing negative numbers | ||
| // When passed to the sum function | ||
| // Then it should still return the correct total sum | ||
|
|
||
| it("array with negative numbers returns correct sum", () => { | ||
| const list = [-1, -2, -3]; | ||
| expect(sum(list)).toEqual(-6); | ||
| }); | ||
|
|
||
| // Given an array with decimal/float numbers | ||
| // When passed to the sum function | ||
| // Then it should return the correct total sum | ||
|
|
||
| it("array with decimal numbers returns correct sum", () => { | ||
| const list = [1.1, 2.2, 3.3]; | ||
| expect(sum(list)).toEqual(6.6); | ||
| }); | ||
|
Comment on lines
+43
to
+46
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); // 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 | ||
|
|
||
| it("array with non-number values returns sum of numerical values only", () => { | ||
| const list = [1, 2, "apple", 4, "banana", 3]; | ||
| expect(sum(list)).toEqual(10); | ||
| }); | ||
|
|
||
| // 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 | ||
|
|
||
| it("array with non-number values only returns least surprising output", () => { | ||
| const list = ["a", "b", "c"]; | ||
| expect(sum(list)).toEqual(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.
Could also include numerical string to ensure value like
"123"is ignored.