-
-
Notifications
You must be signed in to change notification settings - Fork 286
Sheffield | 25-ITP-Sep | Jak Rhodes-Smith | Sprint 2 | Data Groups #919
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
Closed
Closed
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4f5f09b
complete /debug exercises
jakr-s e1c9439
implement contains function with tests
jakr-s e1377fc
refactor: remove unnecessary comments from contains function
jakr-s 7bf8b1a
implement createLookup function and tests
jakr-s a9e23c2
refactor: remove unnecessary comments, put tests in describe block
jakr-s 9559666
implement tally function with tests
jakr-s d7107fd
fix: correct parseQueryString and implement additional tests
jakr-s 5afff25
correct invert function implementation and add tests
jakr-s 21c0ee2
fix: update contains function to use Object.hasOwn
jakr-s 27c9c9d
fix: decode keys and values in parseQueryString and add test case
jakr-s ae23446
fix: update tally function to create empty object and add test for in…
jakr-s 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
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,9 @@ | ||
| function contains() {} | ||
| function contains(obj, property) { | ||
| if (obj === null || obj === undefined) { | ||
| return false; | ||
| } | ||
|
|
||
| return property in obj; | ||
| } | ||
|
|
||
| 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,25 @@ | ||
| 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", () => { | ||
| test("contains returns true for existing property", () => { | ||
| expect(contains({ a: 1, b: 2 }, "a")).toBe(true); | ||
| expect(contains({ a: 1, b: 2 }, "c")).toBe(false); | ||
| }); | ||
|
|
||
| test("contains on empty object returns false", () => { | ||
| expect(contains({}, "a")).toBe(false); | ||
| }); | ||
|
|
||
| test("contains returns true for multiple properties", () => { | ||
| expect(contains({ name: "John", age: 30, city: "NY" }, "age")).toBe(true); | ||
| }); | ||
|
|
||
| test("contains returns false for non-existent property", () => { | ||
| expect(contains({ a: 1, b: 2 }, "c")).toBe(false); | ||
| }); | ||
|
|
||
| test("contains returns false for non-object types", () => { | ||
| expect(contains(null, "a")).toBe(false); | ||
| expect(contains(undefined, "a")).toBe(false); | ||
| }); | ||
| }); |
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,5 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(arr) { | ||
| return Object.fromEntries(arr); | ||
| } | ||
|
|
||
| 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,24 @@ | ||
| 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 input = [ | ||
| ["US", "USD"], | ||
| ["CA", "CAD"], | ||
| ]; | ||
| const result = createLookup(input); | ||
| expect(result).toEqual({ US: "USD", CA: "CAD" }); | ||
| }); | ||
|
|
||
| test("creates a lookup with a single code pair", () => { | ||
| const input = [["GB", "GBP"]]; | ||
| const result = createLookup(input); | ||
| expect(result).toEqual({ GB: "GBP" }); | ||
| }); | ||
|
|
||
| test("creates an empty lookup with an empty array", () => { | ||
| const input = []; | ||
| const result = createLookup(input); | ||
| expect(result).toEqual({}); | ||
| }); | ||
| }); |
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,3 +1,18 @@ | ||
| function tally() {} | ||
| function tally(arr) { | ||
| if (!Array.isArray(arr)) { | ||
| throw new Error(); | ||
| } | ||
|
|
||
| let result = {}; | ||
|
jakr-s marked this conversation as resolved.
Outdated
|
||
|
|
||
| for (const element of arr) { | ||
| if (element in result) { | ||
| result[element] += 1; | ||
| } else { | ||
| result[element] = 1; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| module.exports = tally; | ||
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,34 +1,23 @@ | ||
| const tally = require("./tally.js"); | ||
|
|
||
| /** | ||
| * tally array | ||
| * | ||
| * In this task, you'll need to implement a function called tally | ||
| * that will take a list of items and count the frequency of each item | ||
| * in an array | ||
| * | ||
| * For example: | ||
| * | ||
| * tally(['a']), target output: { a: 1 } | ||
| * tally(['a', 'a', 'a']), target output: { a: 3 } | ||
| * tally(['a', 'a', 'b', 'c']), target output: { a : 2, b: 1, c: 1 } | ||
| */ | ||
| describe("tally", () => { | ||
| test("tally on an empty array returns an empty object", () => { | ||
| expect(tally([])).toEqual({}); | ||
| }); | ||
|
|
||
| // Acceptance criteria: | ||
| test("tally with single item returns object with count of 1", () => { | ||
| expect(tally(["a"])).toEqual({ a: 1 }); | ||
| }); | ||
|
|
||
| // 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("tally with multiple items returns counts for each", () => { | ||
| expect(tally(["a", "a", "a"])).toEqual({ a: 3 }); | ||
| expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 }); | ||
| expect(tally(["x", "y", "x", "z", "y", "x"])).toEqual({ x: 3, y: 2, z: 1 }); | ||
| }); | ||
|
|
||
| // 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"); | ||
|
|
||
| // Given an array with duplicate items | ||
| // When passed to tally | ||
| // Then it should return counts for each unique item | ||
|
|
||
| // Given an invalid input like a string | ||
| // When passed to tally | ||
| // Then it should throw an error | ||
| test("tally with invalid input throws an error", () => { | ||
| expect(() => tally("invalid")).toThrow(); | ||
| expect(() => tally(null)).toThrow(); | ||
| expect(() => tally(undefined)).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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| const invert = require("./invert.js"); | ||
|
|
||
| describe("invert", () => { | ||
| test("inverts an empty object", () => { | ||
| const input = {}; | ||
| const result = invert(input); | ||
| expect(result).toEqual({}); | ||
| }); | ||
|
|
||
| test("inverts a single key-value pair", () => { | ||
| const input = { a: 1 }; | ||
| const result = invert(input); | ||
| expect(result).toEqual({ 1: "a" }); | ||
| }); | ||
|
|
||
| test("inverts multiple key-value pairs", () => { | ||
| const input = { a: 1, b: 2 }; | ||
| const result = invert(input); | ||
| expect(result).toEqual({ 1: "a", 2: "b" }); | ||
| }); | ||
|
|
||
| test("inverts an object with string values", () => { | ||
| const input = { x: "hello", y: "world" }; | ||
| const result = invert(input); | ||
| expect(result).toEqual({ hello: "x", world: "y" }); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.