Birmingham | ITP Jan 2026 | Arunkumar Akilan | Sprint 2 | Data Groups#1027
Birmingham | ITP Jan 2026 | Arunkumar Akilan | Sprint 2 | Data Groups#1027arunkumarakilan wants to merge 6 commits intoCodeYourFuture:mainfrom
Conversation
cjyuan
left a comment
There was a problem hiding this comment.
Can you ensure all the code is consistently formatted?
If you had successfully enabled "Format on save" and specified "Prettier" as your default JS formatter, you would not have this issue.
| test("Should throw an error for an invalid parameters", () => { | ||
| const object = [1,2,3,4,5,6,7]; | ||
| const propertyName = "a"; | ||
| expect(() => contains(object, propertyName)).toThrow(); | ||
| }); |
There was a problem hiding this comment.
Array is also an object where it indexes are its keys.
Object.hasOwn([1, 2, 3], "a") evaluates to false because "a" is not a key (index) of the array.
However, Object.hasOwn([1, 2, 3], "0") evaluates to true because "0" is a key (index) of the array.
To test if the implemented function can correctly return false when the first argument is an array,
we should specify an actual key of the array.
There was a problem hiding this comment.
Thanks for the clarification!
I understand that arrays are objects and that
Object.hasOwn([1, 2, 3], "0") returns true because "0" is a valid index.
However, in my implementation I explicitly treat arrays as invalid inputs:
if (typeof object !== "object" || Array.isArray(object) || object === null)
So the function throws an error before reaching Object.keys().
That’s why the test passes — it never evaluates array indexes like "0".
I’ll update either the test or the implementation depending on the expected behaviour
There was a problem hiding this comment.
It's not about your function, it is correctly implemented.
In practice, we prepare tests not just to check our implementation, but also to serve as a guard for future modification. In TDD, the tests will also serve as the spec that define how the function should be implemented.
That's why I keep emphasising the importance of preparing the tests correctly.
|
Changes look good. |
Learners, PR Template
Self checklist
Changelist
🐞 Debug
address.jsauthor.jsrecipe.js⚙️ Implementation
tallyfunction to count occurrences in an arraycontainsfunction to check if object has a propertycreateLookupfunction to convert key-value pairs into an objectparseQueryStringwith edge case handlinginvertfunction to swap keys and values🧪 Testing
Notes
Object.keys,Object.entries, and array iterationQuestions
No questions at the moment