Sheffield | 26-ITP-jan | Richard Frimpong | Sprint 1 | Data Groups#1032
Sheffield | 26-ITP-jan | Richard Frimpong | Sprint 1 | Data Groups#1032Richiealx wants to merge 7 commits intoCodeYourFuture:mainfrom
Conversation
cjyuan
left a comment
There was a problem hiding this comment.
Code is good. I only have a few suggestions.
| // 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 with no duplicates, it returns the same values", () => { | ||
| expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); | ||
| }); |
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?
| test("given an array with non-number values, ignores them 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 20 < "300" evaluates to true.
To test if the function can correctly ignore non-numeric values, you could include a string such as "300" in the test cases.
| test("given an array with decimal numbers, returns the correct sum", () => { | ||
| 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
There was a problem hiding this comment.
Hi @cjyuan,
Thank you for the feedback.
I have updated the code and tests based on your comments:
- removed the unnecessary extra array clone in
median.js - improved the
dedupetest so it checks that the returned array is a different array object - updated the
maxtest to include a numeric string such as"300"to confirm non-number values are ignored - changed the decimal sum test to use
toBeCloseTo(...)for floating-point comparison - updated
sum.jsandmax.jsto useNumber.isFinite(...)for stricter numeric checks - fixed the
dedupe.jsexport so the implement tests run correctly
I re-ran all Sprint 1 test suites and all tests are passing.
There was a problem hiding this comment.
All these changes sounds good, but your actions somehow introduced merge conflicts to the files. Can you resolve the conflicts?
| <<<<<<< HEAD | ||
| // Keep only real numeric values | ||
| const numbersOnly = list.filter( | ||
| (item) => typeof item === "number" && !Number.isNaN(item) | ||
| ); | ||
|
|
||
| // Return null if the array contains no numbers | ||
| ======= | ||
| // filter() returns a new array, so this does not modify the original input | ||
| const numbersOnly = list.filter((item) => Number.isFinite(item)); | ||
|
|
||
| // Return null if there are no numeric values | ||
| >>>>>>> a22ed15 (Address mentor feedback for sprint 1 data groups) |
There was a problem hiding this comment.
This is a unresolved merge conflict. You need to delete the "merge conflict markers" and the code that you do not want to keep.
There are several others on the files in this branch.
Can you fix them?
Learners, PR Template
Self checklist
Changelist
This pull request contains my completed work for Sprint 1 - Data Groups.
Fix
Completed the fix exercise by correcting the implementation of:
Sprint-1/fix/median.jsChanges made:
nullwhen no numeric values are presentImplement
Completed the implement exercises by implementing the required functions in:
Sprint-1/implement/sum.jsSprint-1/implement/max.jsSprint-1/implement/dedupe.jsI also completed the related test files:
Sprint-1/implement/sum.test.jsSprint-1/implement/max.test.jsSprint-1/implement/dedupe.test.jsImplemented functionality includes:
Refactor
Completed the refactor exercise by updating:
Sprint-1/refactor/includes.jsChanges made:
forloop with afor...ofloopTesting
All tests were run locally using: