Repository navigation
Manchester | 26-ITP-Sep | Precious Moses | Sprint 2 | Data Groups #1497
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
b88166f
7158a9b
16a2b1a
b124b4d
fa4aee7
ee40cba
f4617bf
106c55f
9f62098
51be35c
f2a26fc
96cdc69
990790e
8ea5fab
e586a0b
9b77ec8
3ede084
26939b1
c6044d5
8c357a8
df599c5
6440839
dc9fbcd
cbe89f1
0df2538
c08d7ad
f82e064
b6f88e8
b8fe136
625a080
86acb17
dff6f8c
50c1037
53bcc81
40770d8
89aa7c8
55bcc8e
01bf866
3e4344a
b3d7030
4ebef47
cffb378
f6adbf3
c5db3de
f0daded
a45c5ea
41a07a1
1d46b22
69b6210
78a1f82
12a783e
6373435
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(object, property) { | ||
| if (typeof object !== "object" || object === null || Array.isArray(object)) { | ||
| throw new Error("contains requires an object"); | ||
| } | ||
| return Object.hasOwn(object, property); | ||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,13 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(pairs) { | ||
| const lookup = {}; | ||
|
|
||
| for (const pair of pairs) { | ||
| const countryCode = pair[0]; | ||
| const currencyCode = pair[1]; | ||
|
|
||
| lookup[countryCode] = currencyCode; | ||
| } | ||
| return lookup; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,11 @@ | ||
| const createLookup = require("./lookup.js"); | ||
|
|
||
| test.todo("creates a country currency code lookup for multiple codes"); | ||
|
|
||
| /* | ||
|
|
||
| Create a lookup object of key value pairs from an array of code pairs | ||
|
|
||
| Acceptance Criteria: | ||
|
|
||
| Given | ||
| - An array of arrays representing country code and currency code pairs | ||
| e.g. [['US', 'USD'], ['CA', 'CAD']] | ||
|
|
||
| When | ||
| - createLookup function is called with the country-currency array as an argument | ||
|
|
||
| Then | ||
| - It should return an object where: | ||
| - The keys are the country codes | ||
| - The values are the corresponding currency codes | ||
|
|
||
| Example | ||
| Given: [['US', 'USD'], ['CA', 'CAD']] | ||
|
|
||
| When | ||
| createLookup(countryCurrencyPairs) is called | ||
|
|
||
| Then | ||
| It should return: | ||
| { | ||
| 'US': 'USD', | ||
| 'CA': 'CAD' | ||
| } | ||
| */ | ||
| test("creates a country currency code lookup for multiple codes", () => { | ||
| const pairs = [ | ||
| ["US", "USD"], | ||
| ["CA", "CAD"], | ||
| ]; | ||
| const result = createLookup(pairs); | ||
|
|
||
| expect(result).toEqual({ US: "USD", CA: "CAD" }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
| // Below are some test cases the implementation doesn't handle well. | ||
| // Fix the implementation for these tests, and try to think of as many other edge cases as possible - write tests and fix those too. | ||
|
|
||
| const parseQueryString = require("./querystring.js") | ||
| const parseQueryString = require("./querystring.js"); | ||
|
|
||
| test("should parse values containing '='", () => { | ||
| expect(parseQueryString("equation=a=b-2")).toEqual({ | ||
|
|
@@ -46,3 +46,8 @@ test("should store values of a key in an array when the key has 2 or more values | |
| foo: "bar", | ||
| }); | ||
| }); | ||
|
|
||
| // Edge cases | ||
| test("should not crash on malformed percent-encoding", () => { | ||
| expect(parseQueryString("a=%E0%A4%A")).toEqual({ a: "%E0%A4%A" }); | ||
| }); | ||
|
Comment on lines
+50
to
+53
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. Nice edge case consideration.
Author
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. Thank you for the feedback! |
||
| 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("tally requires an array"); | ||
| } | ||
|
|
||
| const counts = {}; | ||
|
|
||
| for (const item of items) { | ||
| if (Object.hasOwn(counts, item)) { | ||
| counts[item] += 1; | ||
| } else { | ||
| counts[item] = 1; | ||
| } | ||
| } | ||
|
Comment on lines
+6
to
+14
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. When using a regular object as a dictionary or map, we should consider whether a key might match the name of an inherited property. For example, does the following function call returns the value you expect? Suggestion:
Author
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. Thank you for the feedback! I have refactored my code. |
||
| return counts; | ||
| } | ||
|
|
||
| module.exports = tally; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,30 @@ | ||
| // Let's define how invert should work | ||
|
|
||
| // Given an object | ||
| // When invert is passed this object | ||
| // 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; | ||
| invertedObj[value] = key; | ||
| } | ||
|
|
||
| return invertedObj; | ||
| } | ||
|
|
||
| module.exports = invert; | ||
|
|
||
| // a) What is the current return value when invert is called with { a : 1 } | ||
| // Current return value is an object, { key: 1 } | ||
|
|
||
| // b) What is the current return value when invert is called with { a: 1, b: 2 } | ||
| // The current return value is an object also, { key: 2 } | ||
|
|
||
| // c) What is the target return value when invert is called with {a : 1, b: 2} | ||
| // The target value is { "1": "a", "2": "b"} | ||
|
|
||
| // c) What does Object.entries return? Why is it needed in this program? | ||
| // Object.entries returns an array of object [key, value] pairs. | ||
| // It converts an object into an array, so the loop works, since plain objects can't be used with loops. | ||
|
|
||
| // d) Explain why the current return value is different from the target output | ||
| // It differs due to the fact that "invertedObj.key" uses the wrong notation, "key", instead of the variable's value. | ||
| // Also, adding brackets to the literal name, [key], would only copy the object not fix it. | ||
|
|
||
| // 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,13 @@ | ||
| const invert = require("./invert"); | ||
|
|
||
| test("swaps the key and value of a single pair", () => { | ||
| expect(invert({ a: 1 })).toEqual({ 1: "a" }); | ||
| }); | ||
|
|
||
| test("swaps every pair when there are multiple pairs", () => { | ||
| expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" }); | ||
| }); | ||
|
|
||
| test("returns an empty object when given an empty object", () => { | ||
| expect(invert({})).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.
Your current function implementation is good, but our tests (in practice) should not only verify our current implementation, but also ensure that future changes do not alter the function's expected behavior.
Arrays are objects, with their indices acting as keys.
A proper test should use a valid key to guarantee the function returns
falsespecifically because the first argument is an array, not because the key is missing.The same also apply to string.
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.
Thank you for the feedback!
I have refactored my code.