Cape Town | 26-ITP-Jan | Pretty Taruvinga | Sprint 2 | Data Groups#1109
Cape Town | 26-ITP-Jan | Pretty Taruvinga | Sprint 2 | Data Groups#1109Pretty548 wants to merge 26 commits intoCodeYourFuture:mainfrom
Conversation
2. correct import path for createLookup in tests 3.add jest and configure test script.
This comment has been minimized.
This comment has been minimized.
4 similar comments
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.
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.
This comment has been minimized.
This comment has been minimized.
Sprint-2/implement/contains.js
Outdated
| @@ -1,3 +1,7 @@ | |||
| function contains() {} | |||
| function contains(obj, key) { | |||
| if (!obj) return false; | |||
There was a problem hiding this comment.
Supposedly, when obj is one of the following types of value, the function should return false.
[1,2,3],123,true,"string"
There was a problem hiding this comment.
Good catch! I updated the function to ensure it only accepts plain objects by checking for non-object types, null, and arrays. It now correctly returns false for values like arrays, numbers, Booleans, and strings.
Sprint-2/implement/contains.js
Outdated
| function contains(obj, key) { | ||
| if (!obj) return false; | ||
|
|
||
| return Object.prototype.hasOwnProperty.call(obj, key); |
There was a problem hiding this comment.
Can also consider Object.hasOwn().
There was a problem hiding this comment.
Thanks! I updated the implementation to use Object.hasOwn() and ensured the function returns false for non-object inputs instead of throwing an error.
| test("returns false for an array", () => { | ||
| expect(contains([], "a")).toBe(false); | ||
| }); | ||
|
|
There was a problem hiding this comment.
This test does not yet confirm that the function correctly returns false when the first argument is an array.
This is because contains([], "a") could also return false simply because "a" is not a key of the array.
Arrays are objects, with their indices acting as keys. A proper test should use a non-empty array along with a valid
key to ensure the function returns false specifically because the input is an array, not because the key is missing.
There was a problem hiding this comment.
Good point! I’ve updated the test to use a non-empty array with a valid index as the key, so it confirms the function returns false specifically because the input is an array.
| pairs.forEach((pair) => { | ||
| if (!pair) return; | ||
|
|
||
| const index = pair.indexOf("="); | ||
|
|
||
| if (index === -1) { | ||
| result[pair] = ""; | ||
| } else { | ||
| const key = pair.slice(0, index); | ||
| const value = pair.slice(index + 1); | ||
| result[key] = value; | ||
| } | ||
| }); |
There was a problem hiding this comment.
For the following function call, does your function return the value you expect?
parseQueryString("key1=value1&&key2=value2")
There was a problem hiding this comment.
Yes, the function correctly handles this case. The double "&&" creates an empty string when splitting, and the check if (!pair) return; ensures it is ignored. The function returns the expected result with both key-value pairs.
| const result = {}; | ||
|
|
||
| items.forEach((item) => { | ||
| if (result[item]) { | ||
| result[item]++; | ||
| } else { | ||
| result[item] = 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.
There was a problem hiding this comment.
Good catch! I updated the implementation to use Object.create(null) so the result object has no inherited properties. This ensures keys like "toString" are handled correctly.
Sprint-2/interpret/invert.js
Outdated
| const invert = require("./invert"); | ||
|
|
||
| test("inverts a simple object", () => { | ||
| expect(invert({ a: 1 })).toEqual({ 1: "a" }); | ||
| }); | ||
|
|
||
| test("inverts an object with multiple key-value pairs", () => { | ||
| expect(invert({ a: 1, b: 2 })).toEqual({ | ||
| 1: "a", | ||
| 2: "b", | ||
| }); | ||
| }); | ||
|
|
||
| test("returns empty object when given an empty object", () => { | ||
| expect(invert({})).toEqual({}); | ||
| }); | ||
|
|
||
| test("handles non-string values as keys in the inverted object", () => { | ||
| expect(invert({ a: 1, b: true, c: null })).toEqual({ | ||
| 1: "a", | ||
| true: "b", | ||
| null: "c", | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Shouldn't this be implemented in invert.test.js?
There was a problem hiding this comment.
Good catch, thanks! You're right — these tests should live in invert.test.js. I'll move them there to keep the test structure consistent and separated from implementation.
…om>Move invert tests from invert.js to invert.test.js
Learners, PR Template
Self checklist
Changelist