-
-
Notifications
You must be signed in to change notification settings - Fork 320
Manchester | 26-ITP-May | Samreen Amjad | Sprint 2 | Exercises #1292
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,5 @@ | ||
| function contains() {} | ||
| function contains(array, target) { | ||
| return array.includes(target); | ||
| } | ||
|
|
||
| module.exports = contains; | ||
| module.exports = contains; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,43 @@ | ||
| const contains = require("./contains.js"); | ||
|
|
||
| /* | ||
| Implement a function called contains that checks an object contains a | ||
| particular property | ||
|
|
||
| E.g. contains({a: 1, b: 2}, 'a') // returns true | ||
| as the object contains a key of 'a' | ||
|
|
||
| E.g. contains({a: 1, b: 2}, 'c') // returns false | ||
| as the object doesn't contains a key of 'c' | ||
| */ | ||
|
|
||
| // 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"); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| describe("contains", () => { | ||
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| it("contains on empty object returns false", () => { | ||
| const object = {}; | ||
| const property = "a"; | ||
| expect(contains(object, property)).toEqual(false); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
| it("object contains property returns true", () => { | ||
| const object = { a: 1, b: 2 }; | ||
| const property = "a"; | ||
| expect(contains(object, property)).toEqual(true); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| it("object does not contain property returns false", () => { | ||
| const object = { a: 1, b: 2 }; | ||
| const property = "c"; | ||
| expect(contains(object, property)).toEqual(false); | ||
| }); | ||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| it("given invalid parameter (an array) returns false or throws an error", () => { | ||
| expect(contains([], [])).toEqual(false); | ||
| expect(contains(["a", 1], 1)).toEqual(false); | ||
| expect(contains({ a: 1, b: 2 }, ["a"])).toEqual(false); | ||
| }); | ||
|
|
||
| it("given null returns false", () => { | ||
| expect(contains(null, "a")).toEqual(false); | ||
| expect(contains({ a: 1, b: 2 }, null)).toEqual(false); | ||
| expect(contains(null, null)).toEqual(false); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(arrayOfArrays) { | ||
| const lookup = {}; | ||
| for (const array of arrayOfArrays) { | ||
| lookup[array[0]] = array[1]; | ||
| } | ||
| return lookup; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,17 @@ | ||
| 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' | ||
| } | ||
| */ | ||
| describe("createLookup", () => { | ||
| it("creates a country currency code lookup for multiple codes", () => { | ||
| const arrayOfArrays = [ | ||
| ["US", "USD"], | ||
| ["CA", "CAD"], | ||
| ["UK", "GBP"], | ||
| ]; | ||
| expect(createLookup(arrayOfArrays)).toEqual({ | ||
| US: "USD", | ||
| CA: "CAD", | ||
| UK: "GBP", | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,14 +3,29 @@ function parseQueryString(queryString) { | |
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
|
|
||
| // Replaces + with space | ||
| queryString = queryString.replaceAll("+", " "); | ||
|
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. Common practice is to declare another variable to store the converted string. |
||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| f (pair !== "") { | ||
| let index = pair.indexOf("="); | ||
| if (!pair.includes("=")) { | ||
| index = pair.length; | ||
|
Comment on lines
+13
to
+15
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. On line 14, checking the value of |
||
| } | ||
| const rawKey = pair.slice(0, index); | ||
| const rawValue = pair.slice(index + 1); | ||
|
|
||
| // Remove percentage-encoded characters | ||
| const key = decodeURIComponent(rawKey); | ||
| const value = decodeURIComponent(rawValue); | ||
|
|
||
| queryParams[key] = value; | ||
| } | ||
| } | ||
|
|
||
| return queryParams; | ||
| } | ||
|
|
||
| module.exports = parseQueryString; | ||
|
|
||
|
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 file should contain Jest test script to test the function define in |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,37 @@ | ||
| // In the prep, we implemented a function to parse query strings. | ||
| // Unfortunately, it contains several bugs! | ||
| // 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") | ||
|
|
||
| test("should parse values containing '='", () => { | ||
| expect(parseQueryString("equation=a=b-2")).toEqual({ | ||
| equation: "a=b-2", | ||
| }); | ||
| }); | ||
|
|
||
| test("should ignore empty key-value pairs", () => { | ||
| expect(parseQueryString("key1=value1&&key2=value2&")).toEqual({ | ||
| key1: "value1", | ||
| key2: "value2", | ||
| }); | ||
| }); | ||
|
|
||
| test("should accept empty string as key or as value", () => { | ||
| expect(parseQueryString("=value")).toEqual({ "": "value" }); | ||
| expect(parseQueryString("key")).toEqual({ key: "" }); | ||
| expect(parseQueryString("key=")).toEqual({ key: "" }); | ||
| expect(parseQueryString("=")).toEqual({ "": "" }); | ||
| }); | ||
|
|
||
| test("should decode percent-encoded characters", () => { | ||
| expect(parseQueryString("%24half=1%2F2")).toEqual({ | ||
| $half: "1/2", | ||
| }); | ||
| }); | ||
|
|
||
| test("should replace '+' by ' '", () => { | ||
| expect(parseQueryString("full+name=John+Doe")).toEqual({ | ||
| "full name": "John Doe", | ||
| }); | ||
| }); | ||
|
|
||
| // Stretch exercise: Handling query strings that contain identical keys | ||
|
|
||
| // Delete this test if you are not working on this optional case | ||
| test("should store values of a key in an array when the key has 2 or more values", () => { | ||
| expect(parseQueryString("key=value1&key=value2&key=value3&foo=bar")).toEqual({ | ||
| key: ["value1", "value2", "value3"], | ||
| foo: "bar", | ||
| }); | ||
| }); | ||
| function parseQueryString(queryString) { | ||
| const queryParams = {}; | ||
|
|
||
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
|
|
||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| if (pair === "") { | ||
| continue; | ||
| } | ||
|
|
||
| const equalIndex = pair.indexOf("="); | ||
|
|
||
| let key; | ||
| let value; | ||
|
|
||
| if (equalIndex === -1) { | ||
| key = pair; | ||
| value = ""; | ||
| } else { | ||
| key = pair.slice(0, equalIndex); | ||
| value = pair.slice(equalIndex + 1); | ||
| } | ||
|
|
||
| key = decodeURIComponent(key.replaceAll("+", " ")); | ||
| value = decodeURIComponent(value.replaceAll("+", " ")); | ||
|
|
||
| queryParams[key] = value; | ||
| } | ||
|
|
||
| return queryParams; | ||
| } | ||
|
|
||
| module.exports = parseQueryString; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,13 @@ | ||
| function tally() {} | ||
|
|
||
| function tally(array) { | ||
| if (!Array.isArray(array)) { | ||
| throw new Error("Invalid array"); | ||
| } | ||
| const count = Object.create(null); | ||
| array.forEach((item) => { | ||
| if (item in count) { | ||
| count[item] += 1; | ||
| } else count[item] = 1; | ||
| }); | ||
| return count; | ||
| } | ||
| 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.
Can this function pass the tests in
contains.test.js?