-
-
Notifications
You must be signed in to change notification settings - Fork 190
West Midlands | ITP-Sept-25 | Georgina Rogers | Sprint 2 | Coursework Sprint 2 #862
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
841dc4c
c6f3d61
b81e384
c00af0f
527ecda
9d2c822
81cbc64
6156a96
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 (obj !== null && typeof obj === 'object' && !Array.isArray(obj)) { | ||
| return Object.prototype.hasOwnProperty.call(obj, prop); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,19 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(pairs) { | ||
| // function accepts one parameter: pairs of country-currency code arrays | ||
| if (!Array.isArray(pairs)) { | ||
| return {}; // Return empty object if input is not an array | ||
| } | ||
|
|
||
| const lookup = {}; // Initialize empty lookup object, key-value pairs stored in this object | ||
| for (const pair of pairs) { | ||
| // Iterate over each pair in the input array | ||
| if (Array.isArray(pair) && pair.length >= 2) { | ||
| const [countryCode, currencyCode] = pair; // Destructuring first two elements of pair array and assigning them to country and currency | ||
| lookup[countryCode] = currencyCode; // Add country-currency pair to the lookup object - later entries overwrite earlier ones if duplicate country codes exist. | ||
| } | ||
| } | ||
| return lookup; | ||
| } | ||
|
|
||
|
|
||
| module.exports = createLookup; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,21 @@ | ||
| function parseQueryString(queryString) { | ||
| const queryParams = {}; | ||
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| if (!queryString || queryString.length === 0) { | ||
| return queryParams; // Return empty object for empty query string | ||
| } | ||
| const keyValuePairs = queryString.split("&"); | ||
| const keyValuePairs = queryString.split("&"); // Split by '&' to get individual key-value pairs | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| // Iterate over each key-value pair | ||
| if (!pair.includes("=")) { | ||
| // Handle missing equals sign | ||
| queryParams[pair] = undefined; // Assign undefined for keys without equals sign | ||
| continue; // Move to the next pair | ||
| } | ||
| const [key, ...rest] = pair.split("="); // array destructured into key and rest of array. | ||
| // Split by '=' to separate key and value. | ||
| const value = rest.join("="); // Join back any '=' in the value using rest operator | ||
| queryParams[key] = value; // Assign key-value pair to the result object | ||
| } | ||
|
Comment on lines
8
to
19
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. Please note that in real querystring, both May I suggest looking up any of these terms, and "How to decode URL encoded string in JS"? |
||
|
|
||
| return queryParams; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,18 @@ | ||
| function tally() {} | ||
| function tally(items) { | ||
| if (!Array.isArray(items)) { | ||
| throw new Error("Input must be an array"); | ||
| } | ||
| const counts = {}; // empty object to store tally of key-value pairs | ||
|
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. |
||
| for (const item of items) { | ||
| // iterates through each element in the items array | ||
| if (counts[item]) { | ||
| // checks if the item already exists as a key in counts object | ||
| counts[item] += 1; // increments the count for that item by 1 | ||
| } else { | ||
| counts[item] = 1; // initializes the count for that item to 1 | ||
| } | ||
| } | ||
| return counts; // returns the final counts object | ||
| } | ||
|
|
||
| module.exports = tally; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,23 +7,38 @@ | |
| // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} | ||
|
|
||
| function invert(obj) { | ||
| // Guard: only accept plain objects (not null, not arrays, not primitives) | ||
| if (obj === null || typeof obj !== 'object' || Array.isArray(obj)) { | ||
| return {}; | ||
| } | ||
|
|
||
| const invertedObj = {}; | ||
|
|
||
| for (const [key, value] of Object.entries(obj)) { | ||
| invertedObj.key = value; | ||
| // Ensure the value is used as a string key (object keys are strings) | ||
| invertedObj[String(value)] = key; | ||
| } | ||
|
|
||
| return invertedObj; | ||
| } | ||
|
|
||
| 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
+26
to
+31
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) refers to the value returned by the original (unmodified) function. The objects you described on line 28 and 31 are not the objects returned by the original function. |
||
|
|
||
| // 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? | ||
| // It returns an array of key-value pairs from the object. | ||
| // It is needed to iterate over each key-value pair in the input object. | ||
|
|
||
| // d) Explain why the current return value is different from the target output | ||
| // Numeric key values are converted to strings in the output object keys. | ||
|
|
||
| // e) Fix the implementation of invert (and write tests to prove it's fixed!) | ||
| // Invert function passes 4 edge case tests. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| const invert = require('./invert'); | ||
|
|
||
| describe('invert', () => { | ||
| test('inverts a single key', () => { | ||
| expect(invert({ a: 1 })).toEqual({ '1': 'a' }); | ||
| }); | ||
|
|
||
| test('inverts multiple keys', () => { | ||
| expect(invert({ a: 1, b: 2 })).toEqual({ '1': 'a', '2': 'b' }); | ||
| }); | ||
|
|
||
| test('duplicate values: last key wins', () => { | ||
| expect(invert({ a: 1, b: 1 })).toEqual({ '1': 'b' }); | ||
| }); | ||
|
|
||
| test('non-object inputs return empty object', () => { | ||
| expect(invert(null)).toEqual({}); | ||
| expect(invert(undefined)).toEqual({}); | ||
| expect(invert(123)).toEqual({}); | ||
| expect(invert([1, 2, 3])).toEqual({}); | ||
| }); | ||
| }); |
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.
indentation is a bit off.