Sheffield | 26-ITP-jan | Richard Frimpong | Sprint 2 | Data Groups#1042
Sheffield | 26-ITP-jan | Richard Frimpong | Sprint 2 | Data Groups#1042Richiealx wants to merge 10 commits intoCodeYourFuture:mainfrom
Conversation
cjyuan
left a comment
There was a problem hiding this comment.
Code looks good. I just have some suggestions.
| for (const ingredient of recipe.ingredients) { | ||
| console.log(ingredient); | ||
| } |
There was a problem hiding this comment.
Since ingredient values are separated by '\n' in the output, we could also use Array.prototype.join() to construct the equivalent string and then output the resulting string.
| } | ||
|
|
||
| // Check if the object has the property as its own key | ||
| return Object.prototype.hasOwnProperty.call(obj, propertyName); |
There was a problem hiding this comment.
Could also use Object.hasOwn().
| // Validate propertyName | ||
| if (typeof propertyName !== "string" || propertyName.length === 0) { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
- Values of non-string type can be used as property names; they are just implicitly converted to equivalent strings. For example,
let obj = {};
obj[3.0] = 12; // 3.0 is treated as "3" when used as key
console.log(obj["3"]); // output 12
console.log(obj[4-1]); // output 12 too
console.log(Object.hasOwn(obj, 3)); // output true
- Empty string can also serve as key.
| // Extract the key and everything after the first "=" as the value | ||
| const key = pair.slice(0, separatorIndex); | ||
| const value = pair.slice(separatorIndex + 1); | ||
|
|
||
| queryParams[key] = value; |
There was a problem hiding this comment.
No change required.
Note:
-
In real query string, both
keyandvalueare percent-encoded or URL encoded in the URL.
For example, the string "5%" is encoded as "5%25". So to get the actual value of "5%25"
(whether it is a key or value in the query string), we need to call a function to decode it. -
You can also explore the
URLSearchParamsAPI.
| const counts = {}; | ||
|
|
||
| // Loop through each item in the array | ||
| for (const item of items) { | ||
| // If the item already exists in the object, increase the count | ||
| if (counts[item]) { | ||
| counts[item] += 1; | ||
| } else { | ||
| // Otherwise initialise it | ||
| counts[item] = 1; | ||
| } | ||
| } | ||
|
|
||
| return counts; |
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.
Learners, PR Template
Self checklist
Changelist
This pull request contains my completed work for Sprint 2 - Data Groups.
Debug
address.js(correct object property access)author.js(correct object iteration)recipe.js(proper formatting of output)Implement
containsusing own-property checking (avoids inherited properties)createLookupto convert key-value pairs into an objecttallyusingObject.create(null)to handle edge cases like"toString"parseQueryStringto:Interpret
invert.jsStretch
countWordswith:"constructor"calculateModeinto smaller helper functions:Testing