-
-
Notifications
You must be signed in to change notification settings - Fork 178
London | 25-ITP-Sep |Imran Mohamed | Sprint 1 | Sprint-1 exercises #802
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
base: main
Are you sure you want to change the base?
Conversation
| if(filteredList.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| //sort the filtered list | ||
| filteredList.sort((a, b) => a - b); | ||
|
|
||
| const isEven = filteredList.length % 2 === 0; | ||
|
|
||
| if(isEven ){ | ||
| const mid1 = filteredList.length / 2 - 1; | ||
| const mid2 = mid1 + 1; | ||
| const median = (filteredList[mid1] + filteredList[mid2]) / 2; | ||
| return median | ||
| }else { | ||
| const mid = Math.floor(filteredList.length / 2); | ||
| const median = filteredList[mid]; | ||
| return median; | ||
| }; | ||
|
|
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.
To more consistently format the code, consider using this VSCode built-in feature to auto format the code: https://code.visualstudio.com/docs/languages/javascript#_formatting
| test("given an array with no duplicates, it returns a copy of the original array", () => { | ||
| expect(dedupe([1,2,3])).toEqual([1,2,3]); | ||
| }); |
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.
The current test code cannot check if the returned array is a copy of the original array because toEqual() compares objects (including arrays) by value. To illustrate,
const A = [2, 3, 1];
const B = [...A]; // B is a copy of A
// This set of code cannot distinguish if the compared objects are the same objects.
expect(A).toEqual(A); // true
expect(A).toEqual(B); // true
In order to check if the returned array is a copy of the original array, we would need additional checks.
Can you find out what code you need to add in order to ensure the returned value is not the original array?
| test("given an array with decimal numbers, returns correct sum",()=>{ | ||
| expect(sum([1.5,2.5,3.5])).toBe(7.5); | ||
| }) |
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 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); // false
Can 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

Self checklist
Changelist
I have implemented the various functions and the associated tests for the functions.
Questions
The instructions for sprint 1 say that the npm install should be run within the sprint 1 folder but when I attempt to use the Testing tab on vscode I am unable to run the tests from within and the output indicates the tests fail to run as it seems to be looking in the root folder for the node module for jest. I was able to run the tests using npm test however the testing functionality afforded by vscode was diminished by that. Could there be a better way of doing this?