Sheffield | 26-ITP-Jan | Karim M'hamdi | Sprint 1 | Module-Data-Groups#1029
Sheffield | 26-ITP-Jan | Karim M'hamdi | Sprint 1 | Module-Data-Groups#1029KKtech06 wants to merge 5 commits intoCodeYourFuture:mainfrom
Conversation
cjyuan
left a comment
There was a problem hiding this comment.
In the "implement" folder, you missed updating the .test.js files accordingly.
|
Function implementations are ok, but the |
|
Hello CJ i have modified the test.js files and ran them thought Jest and got a Pass for all of the test.js files Thank you |
| test("ignores non-number values and returns the max", () => { | ||
| expect(findMax(["hey", 10, "hi", 60, 10])).toBe(60); | ||
| }); |
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 60 < "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.
| test("given only non-number values, returns null", () => { | ||
| expect(findMax(["hey", "hi", null, undefined, NaN])).toBe(null); | ||
| }); |
There was a problem hiding this comment.
The test on line 62 make it looks like the function considers null as the max value.
You can also consider treating an array with only non-number values the same as an array with no numbers, and give all arrays containing no numbers the same treatment?
| test("given decimal numbers, returns the correct total", () => { | ||
| expect(sum([1.5, 2.5, 3])).toBe(7); | ||
| }); |
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
Learners, PR Template
Self checklist
Changelist: I have modified the files and tested them with Jest and got a Pass
Questions
N/A