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 12 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.
There was a problem hiding this comment.
Hi @cjyuan,
Thank you for your feedback.
I’ve now updated my implementation to address all the points raised:
- Updated
containsto use proper own-property checking and improved the tests to cover inherited properties, arrays, non-string keys, and edge cases - Fixed
tallyby using an object with no inherited properties and added an edge-case test for values like "toString" - Improved
querystringto handle URL decoding and additional edge cases - Replaced the remaining
test.todoinlookup.test.jswith full test coverage, including validation and malformed input cases - Reviewed
recipe.jsand applied the suggested improvement usingjoinfor cleaner output
All test suites are now passing with no remaining TODO tests.
I also corrected the branch issue so this PR now only contains Sprint-2 files.
Thanks again for the helpful suggestions.
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.
|
On this PR branch, I don't see any changes made to the files in the Sprint-2 folder (since my previous review). That is, I don't see some of the changes which you said you did:
|
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