-
-
Notifications
You must be signed in to change notification settings - Fork 178
London|ITP-September-2025|Alexandru Pocovnicu|Sprint 1 #806
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?
London|ITP-September-2025|Alexandru Pocovnicu|Sprint 1 #806
Conversation
…median calculation logic
…prove null handling
…edian calculation logic
|
Your PR's title isn't in the expected format. Please check the expected title format, and update yours to match. Reason: Wrong number of parts separated by |s If this PR is not coursework, please add the NotCoursework label (and message on Slack in #cyf-curriculum or it will probably not be noticed). |
|
Your PR's title isn't in the expected format. Please check the expected title format, and update yours to match. Reason: Wrong number of parts separated by |s If this PR is not coursework, please add the NotCoursework label (and message on Slack in #cyf-curriculum or it will probably not be noticed). |
4 similar comments
|
Your PR's title isn't in the expected format. Please check the expected title format, and update yours to match. Reason: Wrong number of parts separated by |s If this PR is not coursework, please add the NotCoursework label (and message on Slack in #cyf-curriculum or it will probably not be noticed). |
|
Your PR's title isn't in the expected format. Please check the expected title format, and update yours to match. Reason: Wrong number of parts separated by |s If this PR is not coursework, please add the NotCoursework label (and message on Slack in #cyf-curriculum or it will probably not be noticed). |
|
Your PR's title isn't in the expected format. Please check the expected title format, and update yours to match. Reason: Wrong number of parts separated by |s If this PR is not coursework, please add the NotCoursework label (and message on Slack in #cyf-curriculum or it will probably not be noticed). |
|
Your PR's title isn't in the expected format. Please check the expected title format, and update yours to match. Reason: Wrong number of parts separated by |s If this PR is not coursework, please add the NotCoursework label (and message on Slack in #cyf-curriculum or it will probably not be noticed). |
|
|
||
| const middleIndex = Math.floor(sortedList.length / 2); | ||
|
|
||
| const median = sortedList.slice(middleIndex)[0]; |
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.
- There is a more compact and more efficient way to access the element at position
middleIndex.
| return []; | ||
| } | ||
| const arrCopy = arr.slice(0); | ||
| console.log(arrCopy); |
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.
Do you need this console.log() statement for the function to work?
| if (arr.length === 0) { | ||
| return []; | ||
| } | ||
| const arrCopy = arr.slice(0); |
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.
Line 7 will create a new array. We don't have to create another array here.
| test("Given an array with no duplicates,return a copy of the original array", () => { | ||
| expect(dedupe([1, "y", 4, 7])).toEqual([1, "y", 4, 7]); | ||
| }); |
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?
| if (elements.length === 0) { | ||
| return -Infinity; | ||
| } else if (elements.length === 1) { | ||
| return elements[0]; | ||
| } | ||
| if (elements.every((element) => typeof element !== "number")) { | ||
| return NaN; | ||
| } | ||
| let filteredElements = elements.filter( | ||
| (element) => typeof element === "number" | ||
| ); | ||
| let orderedElements = filteredElements.sort((a, b) => a - b); | ||
| return orderedElements[orderedElements.length - 1]; |
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.
-
You could also consider treating an array that doesn’t contain any numbers as an empty array; both of them do not contain any numbers.
-
Does your function return the value you expect from the following function calls?
findMax([ "Foo" ]);
findMax([ "Foo", "Bar" ]);
findMax([ NaN, 1, 2, 3, NaN ]);| if (elements.every((element) => typeof element !== "number")) { | ||
| return NaN; | ||
| } | ||
| const filteredElements = elements.filter( | ||
| (element) => typeof element === "number" | ||
| ); |
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.
When an array contains at least one number, the code on lines 8-13 would end up processing the array elements twice.
Can you figure out a more efficient approach that only requires processing the array elements once?
| test("Given an array with decimal/float numbers,return the correct total sum", () => { | ||
| expect(sum([-1.5, 1.5, 4.5])).toEqual(4.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
| if (elements.length === 0) { | ||
| return 0; | ||
| } | ||
| if (elements.length === 1) { | ||
| return elements[0]; | ||
| } |
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.
Why give special treatment to an array with one element?
|
Your PR's title isn't in the expected format. Please check the expected title format, and update yours to match. Reason: Wrong number of parts separated by |s If this PR is not coursework, please add the NotCoursework label (and message on Slack in #cyf-curriculum or it will probably not be noticed). |
|
Your PR's title isn't in the expected format. Please check the expected title format, and update yours to match. Reason: Wrong number of parts separated by |s If this PR is not coursework, please add the NotCoursework label (and message on Slack in #cyf-curriculum or it will probably not be noticed). |
Learners, PR Template
Self checklist
Changelist
I implemented functions to satisfy pre-written test requirements