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
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,17 @@ | ||
| function contains() {} | ||
| function contains(cont, str) { | ||
| // if (cont === "") { | ||
| // return false; | ||
| // } | ||
| 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,66 @@ 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 true as the object contains a key of 'a'", function () { | ||
| const input = contains({ a: 1, b: 2 }, "c"); | ||
| const currentOutput = input; | ||
| const targetOutput = false; | ||
|
Comment on lines
+43
to
+46
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. How do the descriptions for the To "like" or "dislike" this comment, please follow this link |
||
|
|
||
| // 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 containing a value that isn't an object", () => { | ||
| expect(() => contains(["banana"])).toThrow( | ||
| new Error("contains requires an object") | ||
| ); | ||
| }); | ||
| it("throws when given an array containing a value that isn't an object", () => { | ||
| expect(() => contains(123)).toThrow( | ||
| new Error("contains requires an object") | ||
| ); | ||
| }); | ||
|
|
||
| it("throws when given an array containing a value that isn't an object", () => { | ||
| expect(() => contains(null)).toThrow( | ||
| new Error("contains requires an object") | ||
| ); | ||
| }); | ||
|
|
||
| it("throws when given an array containing a value that isn't an object", () => { | ||
| expect(() => contains()).toThrow(new Error("contains requires an object")); | ||
| }); | ||
|
|
||
| it("throws when given an empty array", () => { | ||
| expect(() => contains([])).toThrow( | ||
| new Error("contains requires an object") | ||
| ); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,14 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(countryCurrencyPairs) { | ||
| const lookUp = {}; | ||
| for (const item of countryCurrencyPairs) { | ||
| lookUp[item[0]] = item[1]; | ||
| } | ||
| return lookUp; | ||
| } | ||
|
|
||
| console.log( | ||
| createLookup([ | ||
| ["US", "USD"], | ||
| ["CA", "CAD"], | ||
| ]) | ||
| ); | ||
|
Comment on lines
+8
to
+13
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 appears in the test process output when To "like" or "dislike" this comment, please follow this link |
||
| 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 = {}; | ||
| if (!Array.isArray(arr)) { | ||
| throw new Error("tally requires an array"); | ||
| } | ||
| if (arr.length === 0) { | ||
| return {}; | ||
| } | ||
| 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 |
||
| } | ||
| } | ||
| 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.
Besides arrays and strings, what other iterable types can a
for...ofloop handle?To "like" or "dislike" this comment, please follow this link