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.
| 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.
| 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.
| 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.