Repository navigation
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 6 commits
23f1985
e1e9072
28477c2
fb64876
a9c191f
30719f0
d58a092
f34837d
5a61bb3
6895740
b971c94
a3dea39
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) { | ||
| 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,14 @@ | ||
| function tally() {} | ||
| function tally(items) { | ||
| if (!Array.isArray(items)) { | ||
| throw new Error('tally expects an array'); | ||
| } | ||
| const counts = Object.create(null); | ||
| 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; | ||
|
cjyuan marked this conversation as resolved.
Resolved
|
||
| } | ||
| return counts; | ||
| } | ||
|
|
||
| module.exports = tally; | ||
|
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 file basically remain unchanged since my previous review. The comments I left on this file still hold. The most recent commit (30719f0) that says "Update invert.js" only removed an empty line from the file: |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,25 +5,32 @@ | |
| // 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); | ||
| (inverted[v] = inverted[v] || []).push(key); | ||
| } | ||
|
|
||
| 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"} | ||
|
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"); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.