London | 26-ITP-Jan | Angela McLeary | Sprint 1 | Sprint 1#1129
London | 26-ITP-Jan | Angela McLeary | Sprint 1 | Sprint 1#1129AngelaMcLeary wants to merge 18 commits intoCodeYourFuture:mainfrom
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| // Given an array with no duplicates | ||
| // When passed to the dedupe function | ||
| // Then it should return a copy of the original array | ||
| test("given an array has no duplicates, it returns a copy of the original array", () => { | ||
| expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); | ||
| expect(dedupe([5, 1, 4])).toEqual([5, 1, 4]); | ||
| }); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Hi @cjyuan, Thank you for the feedback. I added an additional assertion to ensure the dedupe function returns a new array instance, not the original array. The previous test only checked for value equality using .toEqual(), which would still pass even if the function simply returned the input array.To properly validate the acceptance criteria, the test is now fortified.
| test("ignores non-number values and returns the max number", () => { | ||
| expect(findMax(["Not", "A", "Number", 75, 85, 105])).toEqual(105); | ||
| }); | ||
|
|
There was a problem hiding this comment.
When a string representing a valid numeric literal (for example, "300") is compared to a number,
JavaScript first converts the string into its numeric equivalent before performing the comparison.
As a result, the expression 20 < "300" evaluates to true.
To test if the function can correctly ignore non-numeric values,
consider including a string such as "300" in the relevant test cases.
There was a problem hiding this comment.
Hi @cjyuan, Thank you for your feedback. I have updated the test to include an edge case for numerical string.
| test("given an array with decimal float numbers, it should return the total sum", () => { | ||
| expect(sum([1.5, 2.5, 3.5])).toEqual(7.5); | ||
| }); |
There was a problem hiding this comment.
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 46.5678 - 46 === 0.5678 is false because 46.5678 - 46 only yield a value that is very close to 0.5678. Even changing the order in which the program add/subtract numbers can yield different values.
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
- Checking equality in floating point arithmetic in JavaScript
- Checking equality in floating point arithmetic with Jest
There was a problem hiding this comment.
Hi @cjyuan, Thank you for your feedback. I understand now why decimal values need special handling. toBeCloseTo() is a better choice because it checks that the result is close enough within a small margin, which avoids issues caused by floating‑point rounding.
…NaN correctly and shadow array
Learners, PR Template
Self checklist
Changelist
This PR is about fixing median.js, Implementing dedupe.js, max.js, sum.js and refactoring include.js.