-
-
Notifications
You must be signed in to change notification settings - Fork 286
London | 26-ITP-Jan | Oussama Mouggal | Sprint 1| Data-Groups #975
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 5 commits
8ef0e3d
4d632dc
77aeaf4
e9f131d
8b8b7a8
87dd826
c6ca2e0
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,9 @@ | ||
| function dedupe() {} | ||
| function dedupe(list) { | ||
| if (!Array.isArray(list)) { | ||
| return []; | ||
| } | ||
|
|
||
| return [...new Set(list)]; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,10 @@ | ||
| function findMax(elements) { | ||
| if (!Array.isArray(elements)) { | ||
| return -Infinity; | ||
| } | ||
|
|
||
| const numbers = elements.filter((element) => typeof element === "number" && !Number.isNaN(element)); | ||
| return numbers.length === 0 ? -Infinity : Math.max(...numbers); | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,11 @@ | ||
| function sum(elements) { | ||
| if (!Array.isArray(elements)) { | ||
| return 0; | ||
| } | ||
|
|
||
| return elements | ||
| .filter((element) => typeof element === "number" && !Number.isNaN(element)) | ||
| .reduce((total, number) => total + number, 0); | ||
| } | ||
|
|
||
| module.exports = sum; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,24 +13,42 @@ 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([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 with negative numbers, returns correct total", () => { | ||
| expect(sum([10, -5, -2, 3])).toBe(6); | ||
| }); | ||
|
|
||
| // Given an array with decimal/float numbers | ||
| // When passed to the sum function | ||
| // Then it should return the correct total sum | ||
| test("given decimal numbers, returns correct total", () => { | ||
| expect(sum([1.5, 2.5, 3.25])).toBe(7.25); | ||
| }); | ||
|
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 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
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. Thank you for the feedback.I updated the decimal test in Sprint-1 to use Jest’s floating-point matcher instead of exact equality. |
||
|
|
||
| // 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 non-number values, ignores them and sums numbers", () => { | ||
| expect(sum(["hey", 10, "hi", 60, 10])).toBe(80); | ||
| expect(sum([null, 4, "5", true, undefined, 6])).toBe(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 | ||
| test("given only non-number values, returns 0", () => { | ||
| expect(sum(["a", null, undefined, false, {}])).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.
Indentation is off.
Have you installed the prettier VSCode extension and enabled "Format on save/paste" on VSCode,
as recommended in
https://github.com/CodeYourFuture/Module-Structuring-and-Testing-Data/blob/main/readme.md
?
If you have enabled "Format on save" but it is not working, it is likely that you haven't assign a formatter for JS file. This could happen if you have zero or multiple extensions that can format .js file.
If you have installed "Prettier" extension. To assign it as the formatter of JS code, you can try:
settings.jsonand set Prettier as the default formatter for JS.See: https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode
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.
Thanks for your valuable feedback.I fixed it and corrected the indentation in median.js and configured workspace VS Code settings to use Prettier as the default formatter for JavaScript with format-on-save/paste enabled.