London | 26-ITP-May | Zadri Abdule | Sprint 2 | Exercises - #1302
Conversation
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("contains with invalid parameters returns false", () => { | ||
| expect(contains([], 'a')).toBe(false); |
There was a problem hiding this comment.
contains([], 'a') could also return false simply because "a" is not a key of the array.
In JS, arrays are also objects, with their indices acting as keys. A proper test should use a non-empty array along with a valid key of the array to ensure the function returns false specifically because the input is an array, not because the key is missing.
| const params = {}; | ||
| if (!queryString) { |
| const counts = {}; | ||
| for (const item of items) { | ||
| // Use the item as the key; this will stringify non-string keys | ||
| const key = String(item); | ||
| counts[key] = (counts[key] || 0) + 1; | ||
| } |
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, or
- use
Object.hasOwn()
Also, could the code work without line 9?
| // {"1":"a"} | ||
|
|
||
| // b) What is the current return value when invert is called with { a: 1, b: 2 } | ||
| // {"1":"a", "2":"b"} |
There was a problem hiding this comment.
The "current return value" in questions (a), (b), and (d) refer to the return value of the original faulty function.
| } | ||
| } else { | ||
| inverted[v] = key; | ||
| } |
There was a problem hiding this comment.
Note: This rewrite version is doing more than just swapping the keys and values. Change is optional.
Supposedly you could change one line of code to fix the problem.
|
The Changelist content is not quite accurate. |
Learners, PR Template
Self checklist
Changelist