-
-
Notifications
You must be signed in to change notification settings - Fork 210
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
Conversation
cjyuan
left a comment
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.
Code is working great.
I only have a few suggestions.
| // Then it should return a copy of the original array | ||
| test("given an array with no duplicates, it returns a copy of the original array", () => { | ||
| expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); | ||
| expect(dedupe(["a", "b", "c"])).toEqual(["a", "b", "c"]); | ||
| }); |
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:
expect(array1).isEqual(array2);
Suggestion: Find out how to test whether two arrays are different arrays in Jest.
| const numbers = elements | ||
| .filter((val) => typeof val === "number") | ||
| .sort((a, b) => a - b); |
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.
- Sorting is unnecessary before calling
Math.max() - Could also consider filter out
NaN(also a value of type number)
| test("given an array with decimal numbers, returns correct sum", () => { | ||
| expect(sum([10.5, 20.3, 5.2])).toBe(36); | ||
| }); |
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.
Decimal numbers in most programming languages (including JavaScript) are internally represented in floating-point format.
Floating-point arithmetic is not exact. For example, the expression 46.5678 - 46 === 0.5678 evaluates to false
because 46.5678 - 46 produces a value that is only very close to 0.5678, not exactly equal.
Even changing the order in which numbers are added or subtracted can lead to slightly different results.
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
- how to check equality in floating-point arithmetic in JavaScript
- how to check equality in floating-point arithmetic using Jest
Learners, PR Template
Self checklist
Changelist
Complete all mandatory tasks