Repository navigation
London | 26-ITP-Sep | Shirin Panahian | Sprint 2| Complete sprint 2 exercises #1516
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
966d236
47f9997
8eade99
cf7eb8c
2d99aef
2efb219
c442abe
48fdeeb
8901aed
8491ca3
a4455f9
1a111e8
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,14 @@ | ||
| function contains() {} | ||
| function contains(cont, str) { | ||
| if (typeof cont !== "object" || cont === null || Array.isArray(cont)) { | ||
| throw new Error("contains requires an object"); | ||
| } | ||
|
|
||
| const keyArray = Object.keys(cont); | ||
| for (const item of keyArray) { | ||
| if (item === str) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,25 +16,67 @@ as an array isn't an object | |
|
|
||
| // Acceptance criteria: | ||
|
|
||
| // Given a contains function | ||
| // When passed an object and a property name | ||
| // Then it should return true if the object contains the property, false otherwise | ||
|
|
||
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| test.todo("contains on empty object returns false"); | ||
| describe("contains", () => { | ||
| test("given an empty object, returns false", function () { | ||
| const input = contains({}, ""); | ||
| const currentOutput = input; | ||
| const targetOutput = false; | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
| // Given a contains function | ||
| // When passed an object and a property name | ||
| // Then it should return true if the object contains the property, false otherwise | ||
| test("given an object, returns true as the object contains a key of 'a'", function () { | ||
| const input = contains({ a: 1, b: 2 }, "a"); | ||
| const currentOutput = input; | ||
| const targetOutput = true; | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| test("given an object, returns false when the object does not contain key 'c'", function () { | ||
| const input = contains({ a: 1, b: 2 }, "c"); | ||
| const currentOutput = input; | ||
| const targetOutput = false; | ||
|
|
||
| // Given a value that isn't an object - an array, a string, a number, | ||
| // null, or no argument at all | ||
| // When passed to contains | ||
| // Then it should throw Error("contains requires an object") | ||
| // (careful: typeof [] and typeof null are both "object") | ||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
| // Given a value that isn't an object - an array, a string, a number, | ||
| // null, or no argument at all | ||
| // When passed to contains | ||
| // Then it should throw Error("contains requires an object") | ||
| // (careful: typeof [] and typeof null are both "object") | ||
| it("throws when given an array", () => { | ||
| expect(() => contains(["banana"], "a")).toThrow( | ||
| "contains requires an object" | ||
| ); | ||
| }); | ||
|
|
||
| it("throws when given a number", () => { | ||
| expect(() => contains(123, "a")).toThrow("contains requires an object"); | ||
| }); | ||
|
|
||
| it("throws when given a string", () => { | ||
| expect(() => contains("hello", "a")).toThrow("contains requires an object"); | ||
| }); | ||
|
|
||
| it("throws when given null", () => { | ||
| expect(() => contains(null, "a")).toThrow("contains requires an object"); | ||
| }); | ||
|
|
||
| it("throws when called without an object argument", () => { | ||
| expect(() => contains(undefined, "a")).toThrow( | ||
| "contains requires an object" | ||
| ); | ||
| }); | ||
|
|
||
| it("throws when given an empty array", () => { | ||
| expect(() => contains([], "a")).toThrow("contains requires an object"); | ||
| }); | ||
|
Comment on lines
+50
to
+81
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. Since Also, a string as first argument is missed in the error cases, and please add it as well. Thank you.
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 your feedback. I've updated the test cases to include the second argument in all cases I'll run the tests again to make sure everything works correctly. |
||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(countryCurrencyPairs) { | ||
| const lookUp = {}; | ||
| for (const item of countryCurrencyPairs) { | ||
| lookUp[item[0]] = item[1]; | ||
| } | ||
| return lookUp; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,44 @@ | ||
| function parseQueryString(queryString) { | ||
| const queryParams = {}; | ||
| const queryParams = Object.create(null); | ||
|
|
||
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| const newQueryStr = queryString.replaceAll("+", " "); | ||
| const keyValuePairs = newQueryStr.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| if (pair.length === 0) { | ||
| continue; | ||
| } | ||
|
|
||
| const separateIndex = pair.indexOf("="); | ||
|
|
||
| let key; | ||
| let value; | ||
|
|
||
| if (separateIndex === -1) { | ||
| key = pair; | ||
| value = ""; | ||
| } else { | ||
| key = pair.slice(0, separateIndex); | ||
| value = pair.slice(separateIndex + 1); | ||
| } | ||
| key = decodeURIComponent(key); | ||
| value = decodeURIComponent(value); | ||
|
|
||
| if (Object.hasOwn(queryParams, key)) { | ||
| if (Array.isArray(queryParams[key])) { | ||
| queryParams[key].push(value); | ||
| } else { | ||
| queryParams[key] = [queryParams[key], value]; | ||
| } | ||
| } else { | ||
| queryParams[key] = value; | ||
| } | ||
| } | ||
|
|
||
| return queryParams; | ||
| } | ||
|
|
||
| module.exports = parseQueryString; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,21 @@ | ||
| function tally() {} | ||
| function tally(arr) { | ||
| const tallyObj = Object.create(null); | ||
| if (!Array.isArray(arr)) { | ||
| throw new Error("tally requires an array"); | ||
| } | ||
| if (arr.length === 0) { | ||
| return {}; | ||
| } | ||
|
Comment on lines
+6
to
+8
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 your future reference, these 3 lines are not needed, because line 2 initialised |
||
| let key; | ||
| let value; | ||
| for (const item of arr) { | ||
| if (Object.hasOwn(tallyObj, item)) { | ||
| tallyObj[item] = tallyObj[item] + 1; | ||
| } else { | ||
| tallyObj[item] = 1; | ||
|
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. What own property does the To "like" or "dislike" this comment, please follow this link
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. Good catch! When using 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 your future reference, the proper way to fix this AI's review is to use |
||
| } | ||
| } | ||
| return tallyObj; | ||
| } | ||
|
|
||
| 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.
Good that you can think of this boundary test case. Well done!