-
-
Notifications
You must be signed in to change notification settings - Fork 273
Sheffield | 26-ITP-Jan | Karim M'hamdi | Sprint 2 | Module-Data-Groups #1061
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,16 @@ | ||
| function contains() {} | ||
| function contains(obj, prop) { | ||
| if (typeof obj !== "object" || obj === null) { | ||
| return false; | ||
| } | ||
| if (typeof prop !== "string") { | ||
| return false; | ||
| } | ||
|
Comment on lines
+5
to
+7
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. Other types of value can also be used as key (property name) -- They are just converted to equivalent string implicitly. So |
||
|
|
||
| if (Array.isArray(obj)) { | ||
| return false; | ||
| } | ||
| return obj.hasOwnProperty(prop); | ||
| } | ||
|
|
||
|
|
||
| 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 an empty object returns false", function () { | ||
| expect(contains({}, "a")).toBe(false); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
| test("contains returns true when property exists", function () { | ||
| expect(contains({ a: 1, b: 2 }, "a")).toBe(true); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| test("contains returns false when property does not exist", function () { | ||
| expect(contains({ a: 1, b: 2 }, "c")).toBe(false); | ||
| }); | ||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("contains returns false when passed an array", function () { | ||
| expect(contains([1, 2, 3], "a")).toBe(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 valid |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(countryCurrencyPairs) { | ||
| const lookup = {}; | ||
|
|
||
| for (const pair of countryCurrencyPairs) { | ||
| lookup[pair[0]] = pair[1]; | ||
| } | ||
| return lookup; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ function parseQueryString(queryString) { | |
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| const [key, value] = pair.split(/=(.*)/s); | ||
| queryParams[key] = value; | ||
|
Comment on lines
-9
to
10
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. For the following function call, does your function return the value you expect? |
||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,13 @@ | ||
| function tally() {} | ||
| function tally(items) { | ||
| if (!Array.isArray(items)) { | ||
| throw new Error('Items must be an array'); | ||
| } | ||
| const counts = {}; | ||
| for (const item of items) { | ||
| counts[item] = (counts[item] || 0) + 1; | ||
| } | ||
| return counts; | ||
|
Comment on lines
+5
to
+9
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 return the value you expect? Suggestion: Look up an approach to create an empty object with no inherited properties. |
||
|
|
||
| } | ||
|
|
||
| module.exports = tally; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| const invert = require("./invert.js"); | ||
|
|
||
| // Test 1 swaps the keys and values | ||
| test("invert swaps keys and values", () => { | ||
| expect(invert({ a: 1, b: 2 })).toEqual({ "1": "a", "2": "b" }); | ||
| }); | ||
|
|
||
| // Test 2 empty object should return empty object | ||
| test("invert on an empty object returns an empty object", () => { | ||
| expect(invert({})).toEqual({}); | ||
| }); | ||
|
|
||
| // Test 3 single key value pair | ||
| test("invert works with a single pair", () => { | ||
| expect(invert({ x: 10 })).toEqual({ "10": "x" }); | ||
| }); |
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 code works.
Here is an alternative worth exploring:
Since ingredient values are separated by '\n' in the output, we could also use
Array.prototype.join()to construct the equivalent string and then output the resulting string.