-
-
Notifications
You must be signed in to change notification settings - Fork 271
London | 26-ITP-January | Mouawia Elkhalifa | Sprint 2 | Data Groups #1077
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,9 @@ | ||
| function contains() {} | ||
| function contains(obj, prop) { | ||
| if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { | ||
| return false; | ||
| } | ||
|
|
||
| return prop in obj; | ||
| } | ||
|
|
||
| module.exports = contains; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,16 +20,27 @@ as the object doesn't contains a key of 'c' | |
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| test.todo("contains on empty object returns false"); | ||
| test("contains on empty object returns false", () =>{ | ||
| expect(contains({}, "a")).toEqual(false) | ||
| }); | ||
|
|
||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
| test("contains an object with propery exist, returns true", () =>{ | ||
| expect(contains({a:1, b:2}, "a")).toEqual(true) | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
|
|
||
| test("return false when the property does not exist", () =>{ | ||
| expect(contains({a:1, b:3}, "c")).toEqual(false) | ||
| }); | ||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("return false when paramters invaliud ", () =>{ | ||
| expect(contains([], "a")).toEqual(false) | ||
| }); | ||
|
Comment on lines
+44
to
+46
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test does not yet confirm that the function correctly returns false when the first argument is an array. Arrays are objects, with their indices acting as keys. A proper test should use a non-empty array along with a valid |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,13 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(countryCurrencyPairs) { | ||
| const lookup = {}; | ||
|
|
||
| for (const [countryCode, countryCurrency] of countryCurrencyPairs) { | ||
| lookup[countryCode] = countryCurrency; | ||
| } | ||
|
|
||
| return lookup; | ||
| } | ||
|
|
||
| module.exports = createLookup; | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,18 @@ | ||
| function parseQueryString(queryString) { | ||
| if (queryString.startsWith("?")) { | ||
| queryString = queryString.substring(1); | ||
| } | ||
|
|
||
| const queryParams = {}; | ||
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
|
|
||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| const [key, ...valueParts] = pair.split("="); | ||
| queryParams[key] = valueParts.join("="); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: (No change required)
|
||
| } | ||
|
|
||
| return queryParams; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,26 @@ | ||
| function tally() {} | ||
| function tally(arr) { | ||
| // invalid input → throw error | ||
| if (!Array.isArray(arr)) { | ||
| throw new Error("Input must be an array"); | ||
| } | ||
|
|
||
| module.exports = tally; | ||
| // empty array → return empty object | ||
| if (arr.length === 0) { | ||
| return {}; | ||
| } | ||
|
|
||
| const result = {}; | ||
|
|
||
| // count items | ||
| for (const item of arr) { | ||
| if (result[item] === undefined) { | ||
| result[item] = 1; | ||
| } else { | ||
| result[item] = result[item] + 1; | ||
| } | ||
| } | ||
|
Comment on lines
+12
to
+21
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the following function call returns the value you expect? Suggestion: Look up an approach to create an empty object with no inherited properties. |
||
|
|
||
| return result; | ||
| } | ||
|
|
||
| module.exports = tally; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,25 +5,63 @@ | |
| // Then it should swap the keys and values in the object | ||
|
|
||
| // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} | ||
|
|
||
| function invert(obj) { | ||
| const invertedObj = {}; | ||
|
|
||
| for (const [key, value] of Object.entries(obj)) { | ||
| invertedObj.key = value; | ||
| if (invertedObj[value] === undefined) { | ||
| invertedObj[value] = key; | ||
| } else if (Array.isArray(invertedObj[value])) { | ||
| invertedObj[value].push(key); | ||
| } else { | ||
| invertedObj[value] = [invertedObj[value], key]; | ||
| } | ||
|
Comment on lines
+12
to
+18
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good job in recognising the possibility of collision and taking an extra step to address it. |
||
| } | ||
|
|
||
| return invertedObj; | ||
| } | ||
|
|
||
| // a) What is the current return value when invert is called with { a : 1 } | ||
|
|
||
| //{"1": "a"}. | ||
| // b) What is the current return value when invert is called with { a: 1, b: 2 } | ||
|
|
||
| //{"1": "a", "2": "b"} | ||
| // c) What is the target return value when invert is called with {a : 1, b: 2} | ||
|
|
||
| //{"1": "a", "2": "b"} | ||
| // c) What does Object.entries return? Why is it needed in this program? | ||
| // Object.entries(obj) returns an array of [key, value] pairs, where each pair is stored as an array. | ||
| // It is needed in this program so we can loop through both the keys and values of the object at the same time when inverting it. | ||
| //it returns an array of arrays, where each inner array is a pair: [key, value]. | ||
| //The current return value is different from the target output because when multiple keys have | ||
| // the same value, they overwrite each other when inverted. This causes some values to be lost. | ||
| // e) Fix the implementation of invert (and write tests to prove it's fixed!) | ||
| function invert(obj) { | ||
| const invertedObj = {}; | ||
|
|
||
| // d) Explain why the current return value is different from the target output | ||
| for (const [key, value] of Object.entries(obj)) { | ||
| // Check if the value already exists as a key in our new object | ||
| if (invertedObj[value] === undefined) { | ||
| // First time seeing this: Save it as a string | ||
| invertedObj[value] = key; | ||
| } else if (Array.isArray(invertedObj[value])) { | ||
| // It's already an array: Add the new key to the list | ||
| invertedObj[value].push(key); | ||
| } else { | ||
| // It's a string: Turn it into an array and add the new key | ||
| invertedObj[value] = [invertedObj[value], key]; | ||
| } | ||
| } | ||
|
|
||
| // e) Fix the implementation of invert (and write tests to prove it's fixed!) | ||
| return invertedObj; | ||
| } | ||
|
|
||
| // --- CONSOLE TESTS --- | ||
| console.log("Test 1 (Simple):", invert({ a: 1 })); | ||
| // Expected: { "1": "a" } | ||
|
|
||
| console.log("Test 2 (No duplicates):", invert({ x: 10, y: 20 })); | ||
| // Expected: { "10": "x", "20": "y" } | ||
|
|
||
| console.log("Test 3 (Collision):", invert({ a: 1, b: 1 })); | ||
| // Expected: { "1": ["a", "b"] } | ||
|
|
||
| console.log("Test 4 (Triple collision):", invert({ a: 1, b: 1, c: 1 })); | ||
| // Expected: { "1": ["a", "b", "c"] } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider the following two approaches for determining if an object contains a property:
Which of these approaches suits your needs better?
For more info, you can look up
JS "in" operator vs Object.hasOwn.