-
-
Notifications
You must be signed in to change notification settings - Fork 270
Sheffield | 26-ITP-jan | Richard Frimpong | Sprint 2 | Data Groups #1042
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
95c4e75
a4f6039
6543d0f
eb9641b
de8ca14
ce21823
7aa84ed
af702c4
12fb231
e296ae7
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,15 +1,21 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // This program should log out the title, how many it serves and the ingredients. | ||
| // Each ingredient should be logged on a new line | ||
| // How can you fix it? | ||
| // Each ingredient should be logged on a new line. | ||
| // The original code tried to print the entire recipe object, | ||
| // which resulted in "[object Object]" instead of the ingredients. | ||
|
|
||
| const recipe = { | ||
| title: "bruschetta", | ||
| serves: 2, | ||
| ingredients: ["olive oil", "tomatoes", "salt", "pepper"], | ||
| }; | ||
|
|
||
| console.log(`${recipe.title} serves ${recipe.serves} | ||
| ingredients: | ||
| ${recipe}`); | ||
| // Print title and serving size | ||
| console.log(`${recipe.title} serves ${recipe.serves}`); | ||
| console.log("ingredients:"); | ||
|
|
||
| // Loop through the ingredients array and print each one | ||
| for (const ingredient of recipe.ingredients) { | ||
| console.log(ingredient); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,27 @@ | ||
| function contains() {} | ||
| /** | ||
| * contains() | ||
| * | ||
| * Checks whether an object contains a specific property. | ||
| * | ||
| * @param {object} obj - The object to check. | ||
| * @param {string} propertyName - The property name we want to check. | ||
| * @returns {boolean} True if the property exists, otherwise false. | ||
| */ | ||
|
|
||
| function contains(obj, propertyName) { | ||
| // Validate that obj is actually an object | ||
| // and not null or an array | ||
| if (obj === null || typeof obj !== "object" || Array.isArray(obj)) { | ||
| return false; | ||
| } | ||
|
|
||
| // Validate propertyName | ||
| if (typeof propertyName !== "string" || propertyName.length === 0) { | ||
| return false; | ||
| } | ||
|
Comment on lines
+18
to
+21
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.
|
||
|
|
||
| // Check if the object has the property as its own key | ||
| return Object.prototype.hasOwnProperty.call(obj, propertyName); | ||
|
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. Could also use |
||
| } | ||
|
|
||
| module.exports = contains; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,37 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| /** | ||
| * createLookup() | ||
| * | ||
| * Converts an array of [key, value] pairs into a lookup object. | ||
| * | ||
| * Example: | ||
| * [['US', 'USD'], ['CA', 'CAD']] | ||
| * | ||
| * Returns: | ||
| * { US: 'USD', CA: 'CAD' } | ||
| */ | ||
|
|
||
| function createLookup(pairs) { | ||
| // Ensure the input is an array | ||
| if (!Array.isArray(pairs)) { | ||
| throw new Error("Expected an array of pairs"); | ||
| } | ||
|
|
||
| const lookup = {}; | ||
|
|
||
| // Loop through each pair | ||
| for (const pair of pairs) { | ||
| // Validate that each pair has exactly two values | ||
| if (!Array.isArray(pair) || pair.length !== 2) { | ||
| throw new Error("Each item must be a [key, value] pair"); | ||
| } | ||
|
|
||
| const [key, value] = pair; | ||
|
|
||
| // Add to lookup object | ||
| lookup[key] = value; | ||
| } | ||
|
|
||
| return lookup; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,34 @@ | ||
| 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()", () => { | ||
| test("creates a country currency code lookup for multiple codes", () => { | ||
| const pairs = [ | ||
| ["US", "USD"], | ||
| ["CA", "CAD"], | ||
| ]; | ||
|
|
||
| expect(createLookup(pairs)).toEqual({ | ||
| US: "USD", | ||
| CA: "CAD", | ||
| }); | ||
| }); | ||
|
|
||
| test("returns an empty object for an empty array", () => { | ||
| expect(createLookup([])).toEqual({}); | ||
| }); | ||
|
|
||
| test("overwrites duplicate keys with the last value", () => { | ||
| const pairs = [ | ||
| ["US", "USD"], | ||
| ["US", "USN"], | ||
| ]; | ||
|
|
||
| expect(createLookup(pairs)).toEqual({ | ||
| US: "USN", | ||
| }); | ||
| }); | ||
|
|
||
| test("throws an error when input is not an array", () => { | ||
| expect(() => createLookup("invalid")).toThrow(); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,43 @@ | ||
| /** | ||
| * parseQueryString() | ||
| * | ||
| * Parses a query string into an object of key-value pairs. | ||
| * | ||
| * Example: | ||
| * parseQueryString("name=Richard&city=Sheffield") | ||
| * returns { name: "Richard", city: "Sheffield" } | ||
| */ | ||
|
|
||
| function parseQueryString(queryString) { | ||
| const queryParams = {}; | ||
| if (queryString.length === 0) { | ||
|
|
||
| // Return an empty object if the input is an empty string | ||
| if (typeof queryString !== "string" || queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
|
|
||
| // Split the full query string into key-value pairs | ||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| // Skip empty pairs, for example from a trailing "&" | ||
| if (pair === "") { | ||
| continue; | ||
| } | ||
|
|
||
| // Find the position of the first "=" | ||
| const separatorIndex = pair.indexOf("="); | ||
|
|
||
| // If there is no "=" sign, treat it as a key with an empty value | ||
| if (separatorIndex === -1) { | ||
| queryParams[pair] = ""; | ||
| continue; | ||
| } | ||
|
|
||
| // Extract the key and everything after the first "=" as the value | ||
| const key = pair.slice(0, separatorIndex); | ||
| const value = pair.slice(separatorIndex + 1); | ||
|
|
||
| queryParams[key] = value; | ||
|
Comment on lines
+37
to
41
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. No change required. Note:
|
||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,44 @@ | ||
| // In the prep, we implemented a function to parse query strings. | ||
| // Unfortunately, it contains several bugs! | ||
| // Below is one test case for an edge case the implementation doesn't handle well. | ||
| // Fix the implementation for this test, and try to think of as many other edge cases as possible - write tests and fix those too. | ||
| const parseQueryString = require("./querystring.js"); | ||
|
|
||
| const parseQueryString = require("./querystring.js") | ||
| describe("parseQueryString()", () => { | ||
| test("parses querystring values containing =", () => { | ||
| expect(parseQueryString("equation=x=y+1")).toEqual({ | ||
| equation: "x=y+1", | ||
| }); | ||
| }); | ||
|
|
||
| test("returns an empty object for an empty string", () => { | ||
| expect(parseQueryString("")).toEqual({}); | ||
| }); | ||
|
|
||
| test("parses a single key-value pair", () => { | ||
| expect(parseQueryString("name=Richard")).toEqual({ | ||
| name: "Richard", | ||
| }); | ||
| }); | ||
|
|
||
| test("parses multiple key-value pairs", () => { | ||
| expect(parseQueryString("name=Richard&city=Sheffield")).toEqual({ | ||
| name: "Richard", | ||
| city: "Sheffield", | ||
| }); | ||
| }); | ||
|
|
||
| test("handles a key with an empty value", () => { | ||
| expect(parseQueryString("name=")).toEqual({ | ||
| name: "", | ||
| }); | ||
| }); | ||
|
|
||
| test("handles a key with no equals sign", () => { | ||
| expect(parseQueryString("name")).toEqual({ | ||
| name: "", | ||
| }); | ||
| }); | ||
|
|
||
| test("parses querystring values containing =", () => { | ||
| expect(parseQueryString("equation=x=y+1")).toEqual({ | ||
| "equation": "x=y+1", | ||
| test("ignores an empty trailing pair", () => { | ||
| expect(parseQueryString("name=Richard&")).toEqual({ | ||
| name: "Richard", | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,33 @@ | ||
| function tally() {} | ||
| /** | ||
| * tally() | ||
| * | ||
| * Counts how many times each item appears in an array. | ||
| * | ||
| * Example: | ||
| * tally(['a','a','b','c']) | ||
| * returns { a: 2, b: 1, c: 1 } | ||
| */ | ||
|
|
||
| function tally(items) { | ||
| // Validate input | ||
| if (!Array.isArray(items)) { | ||
| throw new Error("Expected an array"); | ||
| } | ||
|
|
||
| const counts = {}; | ||
|
|
||
| // Loop through each item in the array | ||
| for (const item of items) { | ||
| // If the item already exists in the object, increase the count | ||
| if (counts[item]) { | ||
| counts[item] += 1; | ||
| } else { | ||
| // Otherwise initialise it | ||
| counts[item] = 1; | ||
| } | ||
| } | ||
|
|
||
| return counts; | ||
|
Comment on lines
+17
to
+30
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: Look up an approach to create an empty object with no inherited properties. |
||
| } | ||
|
|
||
| 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.
Since ingredient values are separated by '\n' in the output, we could also use
Array.prototype.join()to construct the equivalent string and then output the resulting string.