-
-
Notifications
You must be signed in to change notification settings - Fork 281
London | 26-ITP-Jan | Miriam Jorna | Sprint 2 | Data Groups #1190
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
b9c0a55
a3b820b
f5c978a
30de7d5
b3b0ea7
0c8870b
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, property) { | ||
| if (Array.isArray(obj) || typeof obj !== "object" || obj === null) { | ||
| return false; | ||
| } | ||
| return obj.hasOwnProperty(property); | ||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(pairs) { | ||
| const lookup = {}; | ||
| for (const [country, currency] of pairs) { | ||
| lookup[country] = currency; | ||
| } | ||
| return lookup; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,11 @@ function parseQueryString(queryString) { | |
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| const index = pair.indexOf("="); | ||
| if (index === -1) continue; // skip pairs with no "=" | ||
| const key = pair.slice(0, index); | ||
| const value = pair.slice(index + 1); | ||
|
Comment on lines
+9
to
+12
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 each of the following function calls, does your function return the value you expect? parseQueryString("key1=value1&=&key2=value2")
parseQueryString("key=")
parseQueryString("key1=value1&key2")
parseQueryString("=value")
parseQueryString("key1=value1&&key2=value2")
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. Hmmm, that gets messy quickly. I've added measures to ignore pairs with no = and empty keys. |
||
| if (key === "") continue; // skip empty keys | ||
| queryParams[key] = value; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,16 @@ | ||
| function tally() {} | ||
| function tally(arr) { | ||
| if (!Array.isArray(arr)) { | ||
| throw new Error("Input must be an array"); | ||
| } | ||
| const result = {}; | ||
| for (const item of arr) { | ||
| if (Object.hasOwn(result, item)) { | ||
| result[item]++; | ||
| } else { | ||
| result[item] = 1; | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+5
to
+13
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:
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. I chose Object.hasOwn() |
||
| return result; | ||
|
|
||
| module.exports = tally; | ||
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 function is correct, but we should prepare tests not only to verify our current implementation, but also to ensure that future changes do not alter the function's expected behavior.
This test cannot yet confirm that the function correctly returns false when the first argument is an array.
This is because
contains([1, 2, 3], "a")could also returnfalsesimply because "a" is not a key of the array.Arrays are objects, with their indices acting as keys. A proper test should use a valid key to ensure the function returns
falsespecifically because the input is an array, not because the key is missing.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.
Changed "a" into "0" because "0" exists as a key in the array, so if
contains
returns false it proves the function is correctly rejecting arrays as invalid input.