London | 26-ITP-Jan | Damian Dunkley | Sprint 2 | Data-Groups#993
London | 26-ITP-Jan | Damian Dunkley | Sprint 2 | Data-Groups#993DamianDL wants to merge 23 commits intoCodeYourFuture:mainfrom
Conversation
… function to remember key syntax and functionality
This comment has been minimized.
This comment has been minimized.
4 similar comments
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.
1 similar comment
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
cjyuan
left a comment
There was a problem hiding this comment.
You did a good job in rejecting invalid parameters in all functions.
I only have some suggestions.
| !!obj && // confirm that obj is not null or undefined. If undefined or null, return false. | ||
| typeof obj === "object" && // confirm that obj is an object. If not an object, return false. | ||
| !Array.isArray(obj) && // confirm that obj is not an array. If an array, return false. | ||
| Object.prototype.hasOwnProperty.call(obj, prop)// check if the object has the property. If it does, return true; otherwise, return false. |
There was a problem hiding this comment.
Could also use Object.hasOwn().
There was a problem hiding this comment.
Removed the for loop, replaced with a console.log... .join construction.
There was a problem hiding this comment.
Removed the for loop, replaced with a console.log... .join construction.
Changed Object.prototype.hasOwnProperty() with just Object.hasOwnProperty() to simplify code
There was a problem hiding this comment.
I meant, we can also use Object.hasOwn(obj, prop).
| // Then it should return false or throw an error | ||
| test("contains returns false for invalid input types", () => { | ||
| const arr = [1, 2, 3]; | ||
| expect(contains(arr, "0")).toBe(false); // Arrays do not have properties like objects, so it should return false |
There was a problem hiding this comment.
Note: An array is a kind of objects and its indexes are its keys (properties).
So for an implementation that does not check if the given value is an array using Array.isArray(), contains([1, 2, 3], "0") could have returned true.
There was a problem hiding this comment.
I think that you're saying that my use of !Array.isArray(obj) as a check only works because I have included the property and if I had !Array.isArray(), this would allow 0 to be found in an array of 1, 2, 3 because 1 has a property of "0". Have I understood that correctly or are you expecting me to make changes? Thanks
There was a problem hiding this comment.
Sorry that my comment wasn't clear enough.
I meant say the description given in the comment is not quite correct.
// Arrays do not have properties like objects, so it should return false
The function should return false because the first argument is an array (and not because it does not have properties).
…just Object.hasOwnProperty() to simplify code
|
Changes look good. Well done. |
|
Closing PR because the January ITP run has finished. Feel free to re-open if you're still working on it. |
Learners, PR Template
Self checklist
Changelist
All Sprint 2 exercises updated and code changed
Questions
Please review. If Invert is not correct- changing the function to make it work and putting in naive consol.log tests, please provide some details of what is required? Thanks