Repository navigation
London | 26-ITP-Sep | Shirin Panahian | Sprint 2| Complete sprint 2 exercises - #1516
shirinpanahian wants to merge 10 commits into
Conversation
|
Thank you for this PR. Great work completing the Sprint 2 exercises and adding tests. I can see two small JavaScript edge cases to fix before merging: tally.js (line 12): Plain objects inherit built-in keys like toString, so tally(["toString"]) tries to add 1 to a function instead of returning { toString: 1 }. Using Object.hasOwn() or Object.create(null) fixes this. querystring.js (line 38): Assigning "proto" directly to a normal object changes its prototype instead of saving it as a key. Using Object.create(null) or Object.defineProperty() handles it safely. |
|
Thank you for the feedback and the helpful explanations! I've fixed both edge cases using I've also added tests to cover these cases and pushed the updates to the PR. Thanks again for the review!
|
| if (Object.hasOwn(tallyObj, item)) { | ||
| tallyObj[item] = tallyObj[item] + 1; | ||
| } else { | ||
| tallyObj[item] = 1; |
There was a problem hiding this comment.
What own property does the {} accumulator get when an input item is "__proto__"? Try tally(["__proto__"]) and compare the result with the requirement to count every item.
To "like" or "dislike" this comment, please follow this link
There was a problem hiding this comment.
Good catch! When using {} as the accumulator, __proto__ is treated as a special property rather than a normal own property. This means tally(["__proto__"]) does not correctly count the item. I've fixed this by using Object.create(null) to create an accumulator without a prototype, allowing __proto__ to be counted as a normal key.
There was a problem hiding this comment.
For your future reference, the proper way to fix this AI's review is to use tallyObj.hasOwnProperty(item) which would not modify nor create side effect to the prototype definition.
| console.log( | ||
| createLookup([ | ||
| ["US", "USD"], | ||
| ["CA", "CAD"], | ||
| ]) | ||
| ); |
There was a problem hiding this comment.
What appears in the test process output when lookup.test.js requires this module?
To "like" or "dislike" this comment, please follow this link
There was a problem hiding this comment.
I've removed the console.log() so requiring the module no longer produces unexpected output.
| test("given an object, returns true as the object contains a key of 'a'", function () { | ||
| const input = contains({ a: 1, b: 2 }, "c"); | ||
| const currentOutput = input; | ||
| const targetOutput = false; |
There was a problem hiding this comment.
How do the descriptions for the "c", numeric, null, and missing-argument cases compare with their inputs and assertions?
To "like" or "dislike" this comment, please follow this link
There was a problem hiding this comment.
Thanks for pointing this out. Some of the test descriptions didn't match their actual inputs and assertions. I've updated them so the "c" case describes returning false for a missing property, the numeric case describes receiving a number, the null case describes receiving null, and the missing-argument case describes being called without an argument.
| console.log(author[value]); | ||
| } | ||
| //author is Object and it is not iterable | ||
| //for...of loop excepts array and string but it not working with object type |
There was a problem hiding this comment.
Besides arrays and strings, what other iterable types can a for...of loop handle?
To "like" or "dislike" this comment, please follow this link
There was a problem hiding this comment.
Besides arrays and strings, for...of can iterate over Maps, Sets, TypedArrays, Generators and other iterable objects such as NodeLists.
A normal object isn't iterable by default, which is why for...of doesn't work directly with author. We can use for...in to iterate over its property keys, or Object.values(author) to get its values and iterate over them using for...of.
There was a problem hiding this comment.
Your explanation is correct. Just the typo makes AI misunderstood your comment: should be expects but not excepts.
|
The CYF AI review has left comments. It will only review a PR one time. When you have addressed these comments, please request review again and a volunteer will take a look. |
hackertainment
left a comment
There was a problem hiding this comment.
Good work. Code/comment change required for the following 3 files:
Sprint-2/debug/address.jsSprint-2/debug/recipe.jsSprint-2/implement/contains.test.js
Other review comments are for your future improvement. Thank you.
| console.log(author[value]); | ||
| } | ||
| //author is Object and it is not iterable | ||
| //for...of loop excepts array and string but it not working with object type |
There was a problem hiding this comment.
Your explanation is correct. Just the typo makes AI misunderstood your comment: should be expects but not excepts.
| @@ -1,8 +1,8 @@ | |||
| // Predict and explain first... | |||
There was a problem hiding this comment.
Please add your prediction and explanation as comment into the code (similar to author.js). Thank you.
| // if (cont === "") { | ||
| // return false; | ||
| // } |
There was a problem hiding this comment.
please remove unused code in the future. Thank you.
| test.todo("contains on empty object returns false"); | ||
| describe("contains", () => { | ||
| test("given an empty object, returns false", function () { | ||
| const input = contains({}, ""); |
There was a problem hiding this comment.
Good that you can think of this boundary test case. Well done!
| // Given a value that isn't an object - an array, a string, a number, | ||
| // null, or no argument at all | ||
| // When passed to contains | ||
| // Then it should throw Error("contains requires an object") | ||
| // (careful: typeof [] and typeof null are both "object") | ||
| it("throws when given an array containing a value that isn't an object", () => { | ||
| expect(() => contains(["banana"])).toThrow( | ||
| new Error("contains requires an object") | ||
| ); | ||
| }); | ||
| it("throws when given a number", () => { | ||
| expect(() => contains(123)).toThrow( | ||
| new Error("contains requires an object") | ||
| ); | ||
| }); | ||
|
|
||
| it("throws when given null", () => { | ||
| expect(() => contains(null)).toThrow( | ||
| new Error("contains requires an object") | ||
| ); | ||
| }); | ||
|
|
||
| it("throws when called without an argument", () => { | ||
| expect(() => contains()).toThrow(new Error("contains requires an object")); | ||
| }); | ||
|
|
||
| it("throws when given an empty array", () => { | ||
| expect(() => contains([])).toThrow( | ||
| new Error("contains requires an object") | ||
| ); | ||
| }); |
There was a problem hiding this comment.
Since contains() accepts two mandatory arguments, the test cases should have a proper second argument (even the first argument isn't an object).
Also, a string as first argument is missed in the error cases, and please add it as well. Thank you.
There was a problem hiding this comment.
Thank you for your feedback. I've updated the test cases to include the second argument in all cases I'll run the tests again to make sure everything works correctly.
| if (arr.length === 0) { | ||
| return {}; | ||
| } |
There was a problem hiding this comment.
For your future reference, these 3 lines are not needed, because line 2 initialised tallyObj as an empty {}, and the for-of loop in line 11 will not run when arr is empty, which results in empty {} to be returned correctly in line 18.
| if (Object.hasOwn(tallyObj, item)) { | ||
| tallyObj[item] = tallyObj[item] + 1; | ||
| } else { | ||
| tallyObj[item] = 1; |
There was a problem hiding this comment.
For your future reference, the proper way to fix this AI's review is to use tallyObj.hasOwnProperty(item) which would not modify nor create side effect to the prototype definition.
Added prediction and explanation comments for code behavior.
Learners, PR Template
Self checklist
Task code
CYF-1083
Changelist
This PR includes my work for Sprint 2 of the Module Data Groups project.
contains,lookup,tally, andquerystring. Tested the functions with different inputs to verify their behaviour.console.log().This sprint helped me improve my JavaScript problem-solving, debugging, implementation, and code-reading skills.