West-Midlands | 26-ITP-May | Maryam Janjua | Sprint 2 | Sprint 2 Exercises - #1433
West-Midlands | 26-ITP-May | Maryam Janjua | Sprint 2 | Sprint 2 Exercises#1433maryam-devio wants to merge 4 commits into
Conversation
| let getKey = Object.keys(obj); | ||
| for (let element of getKey){ | ||
| if(element === item){ | ||
| return true | ||
| } | ||
| } | ||
| return false; | ||
| } |
There was a problem hiding this comment.
This works.
Do check out Object.hasOwn() and also
use AI to find out the trade-off among different ways to check if an object contains a particular key.
There was a problem hiding this comment.
Thanks! I checked Object.hasOwn(). I could simplify my function by using Object.hasOwn(obj, item) instead of getting all the keys and looping through them. It also checks only the object's own properties, which fits this function well. I'll also look into the trade-offs between Object.hasOwn(), in, and hasOwnProperty().
| test("return false if it's not an object", () => { | ||
| expect(contains([], 6)).toBe(false); | ||
| }); No newline at end of file |
There was a problem hiding this comment.
When a function does not test if the first argument is an array, contains([], 6) could also return false simply because 6 is not a key of the empty array.
A proper test should use a non-empty array along with a valid
key to ensure the function returns false specifically because the first argument is an array, not because the key is missing.
There was a problem hiding this comment.
Thanks, that makes sense. Using an empty array could return false simply because the key doesn't exist. I've changed the test to use a non-empty array with an existing key so it specifically checks that arrays are rejected.
| key = decodeURIComponent(key); | ||
| value = decodeURIComponent(value); | ||
|
|
||
| if (Object.prototype.hasOwnProperty.call(queryParams, key)) { |
There was a problem hiding this comment.
Could also use Object.hasOwn().
cjyuan
left a comment
There was a problem hiding this comment.
Changes look good.
Thanks for being so thorough with the review feedback and making sure every comment was addressed.
Excellent job!
Learners, PR Template
Self checklist
Changelist
I attempted all the exercises according to the requirements. I debugged the code, completed the tasks, and tested my solutions to make sure they work as expected.