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 1 commit into
Conversation
| ingredients: | ||
| ${recipe}`); | ||
| console.log(`${recipe.title} serves ${recipe.serves}`); | ||
| for(let values of Object.values(recipe.ingredients)){ |
There was a problem hiding this comment.
-
Is it necessary to use
Object.values()on line 18? -
If we do not need to reassign a value to the loop variable, common practice is to declare it using
const.
There was a problem hiding this comment.
Yes, I agree. Object.values() isn’t necessary because recipe.ingredients is already an array, so we can loop over it directly. I also agree that const is better than let because we don’t reassign the loop variable inside each iteration.
| 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().
| const result = arr.reduce((obj, item) => { | ||
| if (obj [item]) { | ||
| obj[item] = obj[item] + 1; | ||
| } | ||
| else{ | ||
| obj[item] = 1; | ||
| } | ||
| return obj; | ||
| }, {}); |
There was a problem hiding this comment.
Does the following function call returns the value you expect?
tally(["toString", "toString"]);
Suggestion:
- Look up an approach to create an empty object with no inherited properties, or
- use
Object.hasOwn()
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.