-
-
Notifications
You must be signed in to change notification settings - Fork 327
London | 26-ITP-May | D Dl | Sprint 2 | implement #1326
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
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,14 @@ | ||
| function contains() {} | ||
| function contains(keyInput, valueInput) { | ||
| if (Array.isArray(keyInput) == true) { | ||
| return false; | ||
| } else { | ||
| for (const key in keyInput) { | ||
| if (key == valueInput) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,21 @@ | ||
| const contains = require("./contains.js"); | ||
|
|
||
| /* | ||
| Implement a function called contains that checks an object contains a | ||
| particular property | ||
| test("Give a contains with object and property and if the object contains the property returns true", () => { | ||
| expect(contains({ a: 1, b: 2 }, "b")).toEqual(true); | ||
| }); | ||
|
|
||
| E.g. contains({a: 1, b: 2}, 'a') // returns true | ||
| as the object contains a key of 'a' | ||
| test("Give a contains with empty object and when passed to contains it returns false", () => { | ||
| expect(contains({}, "y")).toEqual(false); | ||
| }); | ||
|
|
||
| E.g. contains({a: 1, b: 2}, 'c') // returns false | ||
| as the object doesn't contains a key of 'c' | ||
| */ | ||
| test("Give a contains with object and property and if the object contains the property returns true", () => { | ||
| expect(contains({ a: 1, b: 2 }, "a")).toEqual(true); | ||
| }); | ||
|
|
||
| // Acceptance criteria: | ||
| test("Give a contains with object and property and if the property is non-existent returns false", () => { | ||
| expect(contains({ a: 1, b: 2 }, "z")).toEqual(false); | ||
| }); | ||
|
|
||
| // 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 | ||
| test("Give invalid parameter in this case array when passed to contains it returns false", () => { | ||
| expect(contains(["a", 2, "b", 3, 2], 2)).toEqual(false); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| function createLookup() { | ||
| function createLookup(countryCurrencyPairs) { | ||
| // implementation here | ||
| pairs = {}; | ||
| for (const [country, currency] of countryCurrencyPairs) { | ||
| pairs[country] = currency; | ||
| } | ||
| return pairs; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,16 @@ | ||
| 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' | ||
| } | ||
| */ | ||
| test("creates a country currency code lookup for multiple codes", () => { | ||
| expect([["UK", "GBP"]]).toEqual({ UK: "GBP" }); | ||
| }); | ||
| test("creates an empty country currency code returns empty lookup ", () => { | ||
| expect([]).toEqual({}); | ||
| }); | ||
|
|
||
| test("for multiple country currency returns multiple lookup", () => { | ||
| expect([ | ||
| ["UK", "GBP"], | ||
| ["ET", "ETB"], | ||
| ["FR", "EUR"], | ||
| ]).toEqual({ UK: "GBP", ET: "ETB", FR: "EUR" }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,41 @@ | ||
| function parseQueryString(queryString) { | ||
| const queryParams = {}; | ||
| if (queryString.length === 0) { | ||
| if (!queryString) { | ||
| return queryParams; | ||
| } | ||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| // Skip completely empty segments from '&&' or trailing '&' | ||
| if (!pair) continue; | ||
|
|
||
| // 2. Split on '=' separating key and value | ||
| const eqIndex = pair.indexOf("="); | ||
| let rawKey, rawValue; | ||
|
|
||
| if (eqIndex === -1) { | ||
| // Key with no '=' (e.g. "key") | ||
| rawKey = pair; | ||
| rawValue = ""; | ||
| } else { | ||
| rawKey = pair.slice(0, eqIndex); | ||
| rawValue = pair.slice(eqIndex + 1); | ||
| } | ||
|
|
||
| // 3. Replace '+' with ' ' first, then decode URL percent-encoded values | ||
| const key = decodeURIComponent(rawKey.replace(/\+/g, " ")); | ||
| const value = decodeURIComponent(rawValue.replace(/\+/g, " ")); | ||
|
|
||
| // 4. Handle identical keys (stretch goal) | ||
| if (queryParams.hasOwnProperty(key)) { | ||
|
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. Please check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwn, a safer alternative and in the description explained why. |
||
| if (Array.isArray(queryParams[key])) { | ||
| queryParams[key].push(value); | ||
| } else { | ||
| queryParams[key] = [queryParams[key], value]; | ||
| } | ||
| } else { | ||
| queryParams[key] = value; | ||
| } | ||
| } | ||
|
|
||
| return queryParams; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,12 @@ | ||
| function tally() {} | ||
| function tally(items) { | ||
| const result = {}; | ||
| if (!Array.isArray(items)) { | ||
| throw new Error("Invalid input!"); | ||
| } | ||
| for (const item of items) { | ||
| result[item] = (result[item] || 0) + 1; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| module.exports = tally; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,16 +19,25 @@ const tally = require("./tally.js"); | |
| // Given a function called tally | ||
| // When passed an array of items | ||
| // Then it should return an object containing the count for each unique item | ||
| test("given an array of items returns an object containing the counter for each item", () => { | ||
|
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. These tests compare arrays, never call tally |
||
| expect(["a", "a"]).toEqual({ a: 2 }); | ||
| }); | ||
|
|
||
| // Given an empty array | ||
| // When passed to tally | ||
| // Then it should return an empty object | ||
| test.todo("tally on an empty array returns an empty object"); | ||
|
|
||
| test("tally on an empty array returns an empty object", () => { | ||
| expect([]).toEqual({}); | ||
| }); | ||
| // Given an array with duplicate items | ||
| // When passed to tally | ||
| // Then it should return counts for each unique item | ||
|
|
||
| test("given an array of duplicated items returns an object containing the counter for each item", () => { | ||
| expect(["a", "a"]).toEqual({ a: 2 }); | ||
| }); | ||
| // Given an invalid input like a string | ||
| // When passed to tally | ||
| // Then it should throw an error | ||
| test("tally on an empty array returns an empty object", () => { | ||
| expect(() => tally("string").toThrow("Invalid input!")); | ||
|
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. Double check the expression, it contains error
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. hi Selyafi, i have done all tasks under sprint 2 but this time i use one pr for the three section ( debug,implement and interpret) |
||
| }); | ||
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.
For tests please use function
createLookupsince you are testing the functionThere 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.
"test("for multiple country currency returns multiple lookup", createLookup () => {" you meant to use it like this .