generated from CodeYourFuture/Module-Template
-
-
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
Open
Richiealx
wants to merge
13
commits into
CodeYourFuture:main
Choose a base branch
from
Richiealx:coursework/sprint-2-data-groups
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+464
−167
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
95c4e75
Fix address debug exercise
Richiealx a4f6039
Fix author debug exercise
Richiealx 6543d0f
Fix recipe debug exercise
Richiealx eb9641b
Implement contains function and tests
Richiealx de8ca14
Implement lookup function and tests
Richiealx ce21823
Implement tally function and tests
Richiealx 7aa84ed
Fix querystring parser and add edge case tests
Richiealx af702c4
Fix invert implementation and add tests
Richiealx 12fb231
Complete stretch exercises for Sprint 2
Richiealx e296ae7
Fix test file location for countWords
Richiealx a22ed15
Address mentor feedback for sprint 1 data groups
Richiealx d95006e
Remove Sprint-1 files from Sprint-2 PR
Richiealx 4b01ab0
Apply mentor feedback to Sprint 2 files
Richiealx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,19 @@ | ||
| // 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:"); | ||
|
|
||
| // Print each ingredient on a new line using join() | ||
| console.log(recipe.ingredients.join("\n")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,20 @@ | ||
| function contains() {} | ||
| /** | ||
| * contains() | ||
| * | ||
| * Checks whether an object contains a specific own property. | ||
| * | ||
| * @param {object} obj - The object to check. | ||
| * @param {*} propertyName - The property name to check. | ||
| * @returns {boolean} True if the object has the property as its own key, otherwise false. | ||
| */ | ||
| function contains(obj, propertyName) { | ||
| // Reject null, non-objects, and arrays | ||
| if (obj === null || typeof obj !== "object" || Array.isArray(obj)) { | ||
| return false; | ||
| } | ||
|
|
||
| // Check own properties only | ||
| return Object.hasOwn(obj, propertyName); | ||
| } | ||
|
|
||
| module.exports = contains; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,45 @@ | ||
| const contains = require("./contains.js"); | ||
|
|
||
| /* | ||
| Implement a function called contains that checks an object contains a | ||
| particular property | ||
| Implement a function called contains that checks whether an object contains | ||
| a particular own 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' | ||
| E.g. contains({ a: 1, b: 2 }, "a") // returns true | ||
| E.g. contains({ a: 1, b: 2 }, "c") // returns false | ||
| */ | ||
|
|
||
| // 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()", () => { | ||
| test("returns false for an empty object", () => { | ||
| expect(contains({}, "a")).toBe(false); | ||
| }); | ||
|
|
||
| test("returns true when the property exists", () => { | ||
| expect(contains({ a: 1, b: 2 }, "a")).toBe(true); | ||
| }); | ||
|
|
||
| test("returns false when the property does not exist", () => { | ||
| expect(contains({ a: 1, b: 2 }, "c")).toBe(false); | ||
| }); | ||
|
|
||
| test("returns false for inherited properties", () => { | ||
| expect(contains({ a: 1, b: 2 }, "toString")).toBe(false); | ||
| }); | ||
|
|
||
| test("returns false when given an array with a realistic array key", () => { | ||
| expect(contains(["a", "b"], 0)).toBe(false); | ||
| }); | ||
|
|
||
| test("returns false when given null", () => { | ||
| expect(contains(null, "a")).toBe(false); | ||
| }); | ||
|
|
||
| test("supports non-string property names", () => { | ||
| const obj = { 3: 12 }; | ||
| expect(contains(obj, 3)).toBe(true); | ||
| }); | ||
|
|
||
| test("supports empty string as a property name", () => { | ||
| const obj = { "": 99 }; | ||
| expect(contains(obj, "")).toBe(true); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,50 @@ | ||
| // 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("ignores an empty trailing pair", () => { | ||
| expect(parseQueryString("name=Richard&")).toEqual({ | ||
| name: "Richard", | ||
| }); | ||
| }); | ||
|
|
||
| test("parses querystring values containing =", () => { | ||
| expect(parseQueryString("equation=x=y+1")).toEqual({ | ||
| "equation": "x=y+1", | ||
| test("decodes URL-encoded keys and values", () => { | ||
| expect(parseQueryString("tags%5B%5D=hello%20world")).toEqual({ | ||
| "tags[]": "hello world", | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This test can be included in the test category on line 16.
There is no non-string* properties. When we express a property as a non-string value, it is converted to a string. So
"{ 3: 12 }"is identical to{"3": 12}.*: Except for the special type -- Symbol.