Repository navigation
Manchester | 26-ITP-Sep | Precious Moses | Sprint 2 | Data Groups - #1497
moses77-boop wants to merge 48 commits into
Conversation
… parameter handling
…th multiple key-value pairs
…h multiple key-value pairs
… the invert function
| test("contains throws an error for an array", () => { | ||
| expect(() => contains([1, 2, 3], "name")).toThrow( | ||
| "contains requires an object" | ||
| ); | ||
| }); | ||
|
|
||
| test("contains throws an error for a string", () => { | ||
| expect(() => contains("Manchester", "name")).toThrow( | ||
| "contains requires an object" | ||
| ); | ||
| }); |
There was a problem hiding this comment.
Your current function implementation is good, but our tests (in practice) should not only verify our current implementation, but also ensure that future changes do not alter the function's expected behavior.
Arrays are objects, with their indices acting as keys.
Object.hasOwn([1, 2, 3], 'name') // evaluates to false
Object.hasOwn([1, 2, 3], '0') // evaluates to true
A proper test should use a valid key to guarantee the function returns false specifically because the first argument is an array, not because the key is missing.
The same also apply to string.
| // Edge cases | ||
| test("should not crash on malformed percent-encoding", () => { | ||
| expect(parseQueryString("a=%E0%A4%A")).toEqual({ a: "%E0%A4%A" }); | ||
| }); |
There was a problem hiding this comment.
Nice edge case consideration.
| const counts = {}; | ||
|
|
||
| for (const item of items) { | ||
| if (counts[item] === undefined) { | ||
| counts[item] = 1; | ||
| } else { | ||
| counts[item] += 1; | ||
| } | ||
| } |
There was a problem hiding this comment.
When using a regular object as a dictionary or map, we should consider whether a key might match the name of an inherited property.
For example, 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(), or - use a
Mapobject
| // a) What is the current return value when invert is called with { a : 1 } | ||
| // Current return value is 1. | ||
|
|
||
| // b) What is the current return value when invert is called with { a: 1, b: 2 } | ||
| // The current return value is 2. |
There was a problem hiding this comment.
I think the question is asking what the original function returns when the parameter is the specified object.
Does the function return a number or an object?
Self checklist
Task code
CYF-1083
Changelist
In this PR, I debugged, interpreted codes, implemented tests and wrote functions to complete this coursework.