-
-
Notifications
You must be signed in to change notification settings - Fork 321
London | 26-ITP-May | Zadri Abdule | Sprint 2 | Exercises #1302
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,8 @@ | ||
| function contains() {} | ||
| function contains(obj, prop) { | ||
| if (typeof obj !== "object" || obj === null) { | ||
| return false; | ||
| } | ||
| return obj.hasOwnProperty(prop); | ||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,17 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(countryCurrencyPairs) { | ||
| let lookUp = {}; | ||
| if (!Array.isArray(countryCurrencyPairs)) { | ||
| throw new Error("Input must be an array of arrays"); | ||
| } | ||
| countryCurrencyPairs.forEach(pair => { | ||
| if (Array.isArray(pair) && pair.length === 2) { | ||
| lookUp[pair[0]] = pair[1]; | ||
| } else { | ||
| throw new Error("Each element of the input array must be an array of two elements"); | ||
| } | ||
| }); | ||
|
|
||
| return lookUp; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,30 @@ | ||
| function parseQueryString(queryString) { | ||
| const queryParams = {}; | ||
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| const params = {}; | ||
| if (!queryString) { | ||
|
Comment on lines
+2
to
+3
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. Indentation is off. |
||
| return params; | ||
| } | ||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| } | ||
| const pairs = queryString.split("&"); | ||
| for (const pair of pairs) { | ||
| if (!pair) continue; // Ignore empty key-value pairs | ||
|
|
||
| const [key, ...valueParts] = pair.split("="); | ||
| const value = valueParts.join("="); // Join back in case value contains '=' | ||
|
|
||
| return queryParams; | ||
| const decodedKey = decodeURIComponent(key.replace(/\+/g, " ")); | ||
| const decodedValue = decodeURIComponent(value.replace(/\+/g, " ")); | ||
|
|
||
| if (params.hasOwnProperty(decodedKey)) { | ||
| if (Array.isArray(params[decodedKey])) { | ||
| params[decodedKey].push(decodedValue); | ||
| } else { | ||
| params[decodedKey] = [params[decodedKey], decodedValue]; | ||
| } | ||
| } else { | ||
| params[decodedKey] = decodedValue; | ||
| } | ||
| } | ||
| return params; | ||
| } | ||
|
|
||
| module.exports = parseQueryString; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,16 @@ | ||
| function tally() {} | ||
| function tally(items) { | ||
| if (!Array.isArray(items)) { | ||
| throw new Error('tally expects an array'); | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
|
Comment on lines
+6
to
+11
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:
Also, could the code work without line 9? |
||
|
|
||
| return counts; | ||
| } | ||
|
|
||
| module.exports = tally; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,25 +5,44 @@ | |
| // 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 = {}; | ||
| const inverted = {}; | ||
|
|
||
| for (const [key, value] of Object.entries(obj)) { | ||
| invertedObj.key = value; | ||
| const v = String(value); | ||
|
|
||
| if (v in inverted) { | ||
| // If value already exists | ||
| if (Array.isArray(inverted[v])) { | ||
| inverted[v].push(key); | ||
| } else { | ||
| inverted[v] = [inverted[v], key]; | ||
| } | ||
| } else { | ||
| inverted[v] = key; | ||
| } | ||
|
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: 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. |
||
| } | ||
|
|
||
| return invertedObj; | ||
| return inverted; | ||
| } | ||
|
|
||
| module.exports = invert; | ||
|
|
||
| // 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"} | ||
|
Comment on lines
+32
to
+35
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. The "current return value" in questions (a), (b), and (d) refer to the return value of the original faulty function. |
||
|
|
||
| // c) What is the target return value when invert is called with {a : 1, b: 2} | ||
| // the intended result is -> {"1":"a","2":"b"} (same as the as the current output) | ||
|
|
||
| // c) What does Object.entries return? Why is it needed in this program? | ||
| // makes a list of pairs from the object: [[key1, value2], [key2, value2]] | ||
| // it's needed so that the code can look at the key and value together in each loop and build the swapped object. | ||
|
|
||
| // d) Explain why the current return value is different from the target output | ||
| // In the examples shown, it matches the expectation. but: values are turned into strings when used as keys (so 1 becomes "1"). | ||
|
|
||
|
|
||
| // e) Fix the implementation of invert (and write tests to prove it's fixed!) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| const invert = require('./invert'); | ||
|
|
||
| test('inverts a single key/value pair', () => { | ||
| expect(invert({ a: 1 })).toEqual({ '1': 'a' }); | ||
| }); | ||
|
|
||
| test('inverts multiple pairs', () => { | ||
| expect(invert({ a: 1, b: 2 })).toEqual({ '1': 'a', '2': 'b' }); | ||
| }); | ||
|
|
||
| test('string values become keys', () => { | ||
| expect(invert({ a: 'x', b: 'y' })).toEqual({ x: 'a', y: 'b' }); | ||
| }); | ||
|
|
||
| test('collects multiple keys that share the same value', () => { | ||
| const input = { a: 1, b: 1, c: 2 }; | ||
| const actual = invert(input); | ||
| expect(actual["1"]).toEqual(expect.arrayContaining(["a", "b"])); | ||
| expect(actual["2"]).toBe("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.
contains([], 'a')could also returnfalsesimply 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
falsespecifically because the input is an array, not because the key is missing.