-
-
Notifications
You must be signed in to change notification settings - Fork 279
Cape Town | 2026-ITP-Jan | Isaac Abodunrin | Sprint 2 | Grouping data: Objects #1143
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
bytesandroses
wants to merge
61
commits into
CodeYourFuture:main
Choose a base branch
from
bytesandroses:coursework/Sprint-2
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.
Open
Changes from all commits
Commits
Show all changes
61 commits
Select commit
Hold shift + click to select a range
3d7370b
Implementation for median
bytesandroses fdbd966
Test for empty array
bytesandroses a28e904
Test for array without duplicates
bytesandroses 06339fc
Implement dedupe
bytesandroses 4426ed9
Testing for dedupe
bytesandroses 4b1ac0d
Refactor test
bytesandroses b6c0807
Testing for max
bytesandroses aa0f82d
Fix: correct case 2 input to array with single number, not just a sin…
bytesandroses cc952dd
Implement findMax
bytesandroses 8596f7f
Readability: remove unused code
bytesandroses ebbad7e
Fix: reduce number of decimal places returned for sum
bytesandroses 0940103
Refactor to use for of loop
bytesandroses 1c72bab
Debug: address.js
bytesandroses 88d95b1
Debug author
bytesandroses c0f7bb1
Fix: correct for loop
bytesandroses cb55db5
Fix: correct for loop
bytesandroses 81f9946
Fix: print property instead of entire object
bytesandroses 1c5c121
Test: Case 1 -- prop exists in object
bytesandroses b22a517
Fix: correct passing obj and prop into test
bytesandroses 7689546
Test: Case 2 for when property not present in object
bytesandroses ceebce1
Test: Case 3 for empty object
bytesandroses b688460
Test: Case 3 for empty object
bytesandroses fdc221d
Test: Case 4 for invalid objects
bytesandroses c68b85c
Implement contains.js
bytesandroses 49e36bb
Refactor with describe
bytesandroses 7ad5748
Test: add test for Case 2
bytesandroses 34d7c95
Refactor: remove unnecessary try/catch
bytesandroses 47113d8
Refactor for readability
bytesandroses 8aefebd
Fix: reorder validation checks to prevent weird results
bytesandroses ff9eac0
Refactor: remove unnecessary try/catch wrapping
bytesandroses 2743b71
Ensure prototype properties aren't incorrectly returned as true
bytesandroses 672d459
Fix: ensure prototype properties don't return true
bytesandroses e6cde3f
Check for nulls and arrays
bytesandroses c837ff1
Refactor
bytesandroses 273dc37
Test: empty object input
bytesandroses eb1dee9
Test: array without duplicates
bytesandroses 340015c
Implementation of tally function
bytesandroses 420bc1f
Fix: correct tally variable with counts
bytesandroses 8919eac
Test: array with duplicates
bytesandroses 8d56810
Refactor: remove unnecessary debugging code
bytesandroses bf09c40
Test: invalid non array inputs
bytesandroses 7e6ad11
Fix: split key-value pair on only first occurence of '='
bytesandroses 21df2c2
Refactor: test for additional cases
bytesandroses 50e80fd
Test: strings with multiple key-value pairs
bytesandroses a9a06c3
Validate input is string
bytesandroses 774cce1
Ensure that '=' with spaces are processed correctly
bytesandroses 9873f85
Fix: iterate over tests correctly
bytesandroses cd4b7b5
Fix: ensure input is stripped of spaces
bytesandroses 0c144f3
Ensure both key and value are provided
bytesandroses a62b0eb
Ensure that neither the key nor value is made up entirely of whitespace
bytesandroses 35e98cc
Fix: correct implementation and test
bytesandroses a2eb57f
Revert to main for files outside Sprint 2
bytesandroses 2921a9e
Merge branch 'main' into coursework/Sprint-2
bytesandroses 7172685
Revert to main for files outside Sprint 2
bytesandroses 7c8f06d
Implement findMax function in max.js
bytesandroses a005979
Implement findMax function in max.js
bytesandroses e5aee85
Add sum function definition in sum.js
bytesandroses b212457
Fix formatting in sum.test.js
bytesandroses f6e13a9
Fix: ensure that arrays are correctly tested
bytesandroses b058443
Merge branch 'coursework/Sprint-2' of https://github.com/bytesandrose…
bytesandroses c685c0b
Correct target return value
bytesandroses 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,11 @@ | ||
| function contains() {} | ||
| function contains(object, property) { | ||
| if (typeof object !== "object" || object === null || Array.isArray(object)) { | ||
| throw new TypeError( | ||
| "First argument must be an object in the form { key: value }" | ||
| ); | ||
| } | ||
|
|
||
| return object.hasOwnProperty(property); | ||
| } | ||
|
|
||
| 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,78 @@ | ||
| const contains = require("./contains.js"); | ||
|
|
||
| /* | ||
| Implement a function called contains that checks an object contains a | ||
| particular property | ||
| describe("contains", () => { | ||
| // Case 1: Should return true if the property exists in object. | ||
| test("should return true when object contains passed property name", () => { | ||
| const objsWithValidProps = [ | ||
| [{ a: 1, b: 2 }, "b"], | ||
| [{ name: "John", age: 30 }, "name"], | ||
| [{ nested: { key: "value" } }, "nested"], | ||
| [{ id: 123, status: "active", language: "JavaScript" }, "status"], | ||
| [{ data: [], items: null }, "data"], | ||
| ]; | ||
|
|
||
| E.g. contains({a: 1, b: 2}, 'a') // returns true | ||
| as the object contains a key of 'a' | ||
| objsWithValidProps.forEach(([obj, prop]) => { | ||
| expect(contains(obj, prop)).toEqual(true); | ||
| }); | ||
| }); | ||
|
|
||
| E.g. contains({a: 1, b: 2}, 'c') // returns false | ||
| as the object doesn't contains a key of 'c' | ||
| */ | ||
| // Case 2: Should return false if the object does not contain the given property. | ||
| test("should return false when object does not contain passed property name", () => { | ||
| const objsWithoutProps = [ | ||
| [{ a: 1, b: 2 }, "c"], | ||
| [{ name: "John", age: 30 }, "email"], | ||
| [{ nested: { key: "value" } }, "nonexistent"], | ||
| [{ id: 123, status: "active", language: "JavaScript" }, "description"], | ||
| [{ data: [], items: null }, "nonexistent"], | ||
| ]; | ||
|
|
||
| // Acceptance criteria: | ||
| objsWithoutProps.forEach(([obj, prop]) => { | ||
| expect(contains(obj, prop)).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 | ||
| // Case 3: Should return false if the object is empty. | ||
| test("should return false when object is empty", () => { | ||
| expect(contains({}, "anyProperty")).toEqual(false); | ||
| }); | ||
|
|
||
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| test.todo("contains on empty object returns false"); | ||
| // Case 4: Should return false for properties that only exist in the prototype chain | ||
| test("should return false for properties in prototype chain", () => { | ||
| const objsWithProtoProps = [ | ||
| [{ a: 1 }, "toString"], | ||
| [{ name: "John", age: 30 }, "hasOwnProperty"], | ||
| [{ nested: { key: "value" } }, "isPrototypeOf"], | ||
| ]; | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
| objsWithProtoProps.forEach(([obj, prop]) => { | ||
| expect(contains(obj, prop)).toEqual(false); | ||
| }); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| // Case 5: Should throw an error if an array is passed | ||
| test("should throw error when array is passed", () => { | ||
| expect(() => contains(["string"], 0)).toThrow( | ||
| "First argument must be an object in the form { key: value }" | ||
| ); | ||
| }); | ||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| // Case 6: Should throw an error if a non-object is passed | ||
| test("should throw error when non-object is passed", () => { | ||
| const nonObjects = [ | ||
| null, | ||
| undefined, | ||
| 42, | ||
| "The Curse", | ||
| true, | ||
| Infinity, | ||
| Symbol("sym"), | ||
| function () {}, | ||
| ]; | ||
|
|
||
| nonObjects.forEach((nonObj) => { | ||
| expect(() => contains(nonObj, "prop")).toThrow( | ||
| "First argument must be an object in the form { key: value }" | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
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,35 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(countryCurrencyPairs) { | ||
| let countryCurrencyObj = {}; | ||
|
|
||
| for (const countryCurrencyPair of countryCurrencyPairs) { | ||
| if (!Array.isArray(countryCurrencyPair)) { | ||
| throw new Error("Country-currency pairs must be in array format"); | ||
| } | ||
|
|
||
| if (countryCurrencyPair.length !== 2) { | ||
| throw new Error( | ||
| "Country-currency pairs must contain exactly two elements: country and currency" | ||
| ); | ||
| } | ||
|
|
||
| let [country, currency] = countryCurrencyPair; | ||
|
|
||
| if (typeof country !== "string" || typeof currency !== "string") { | ||
| throw new Error("Country-currency pairs must be in string format"); | ||
| } | ||
|
|
||
| if (country.trim() === "" || currency.trim() === "") { | ||
| throw new Error("Country and currency codes cannot be empty"); | ||
| } | ||
|
|
||
| if (countryCurrencyObj[country]) { | ||
| throw new Error(`Duplicate country code found: ${country}`); | ||
| } | ||
|
|
||
| countryCurrencyObj[country] = currency; | ||
| } | ||
|
|
||
| return countryCurrencyObj; | ||
| } | ||
|
|
||
| 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,112 @@ | ||
| const createLookup = require("./lookup.js"); | ||
|
|
||
| test.todo("creates a country currency code lookup for multiple codes"); | ||
| describe("createLookup", () => { | ||
| // Case 1: Returns an empty object if no country-currency pairs are provided | ||
| test("returns an empty object if no country-currency pairs are provided", () => { | ||
| const countryCurrencyPairs = []; | ||
| const currencyObj = createLookup(countryCurrencyPairs); | ||
|
|
||
| /* | ||
| expect(currencyObj).toEqual({}); | ||
| }); | ||
|
|
||
| Create a lookup object of key value pairs from an array of code pairs | ||
| // Case 2: Returns country currency code lookup for a single country-currency pair | ||
| test("creates a country currency code lookup for a single code pair", () => { | ||
| const countryCurrencyPairs = [["US", "USD"]]; | ||
| const currencyObj = createLookup(countryCurrencyPairs); | ||
|
|
||
| Acceptance Criteria: | ||
| expect(currencyObj).toEqual({ | ||
| US: "USD", | ||
| }); | ||
| }); | ||
|
|
||
| Given | ||
| - An array of arrays representing country code and currency code pairs | ||
| e.g. [['US', 'USD'], ['CA', 'CAD']] | ||
| // Case 3: Returns country currency codes lookup for multiple country-currency pairs | ||
| test("creates a country currency code lookup for multiple codes", () => { | ||
| const countryCurrencyPairs = [ | ||
| ["US", "USD"], | ||
| ["CA", "CAD"], | ||
| ["GB", "GBP"], | ||
| ["ZA", "ZAR"], | ||
| ["NG", "NGN"], | ||
| ]; | ||
|
|
||
| When | ||
| - createLookup function is called with the country-currency array as an argument | ||
| const inputCurrencyPairObj = createLookup(countryCurrencyPairs); | ||
| const outputCurrencyPairObj = { | ||
| US: "USD", | ||
| CA: "CAD", | ||
| GB: "GBP", | ||
| ZA: "ZAR", | ||
| NG: "NGN", | ||
| }; | ||
|
|
||
| Then | ||
| - It should return an object where: | ||
| - The keys are the country codes | ||
| - The values are the corresponding currency codes | ||
| expect(inputCurrencyPairObj).toEqual(outputCurrencyPairObj); | ||
| }); | ||
|
|
||
| Example | ||
| Given: [['US', 'USD'], ['CA', 'CAD']] | ||
| // Case 4: Throws an error if a country-currency pair is not an array | ||
| test("throws an error if a country-currency pair is not an array", () => { | ||
| const countryCurrencyPairs = [ | ||
| ["US", "USD"], | ||
| ["CA", "CAD"], | ||
| "GB-GBP", | ||
| ["ZA", "ZAR"], | ||
| ["NG", "NGN"], | ||
| ]; | ||
|
|
||
| When | ||
| createLookup(countryCurrencyPairs) is called | ||
| expect(() => createLookup(countryCurrencyPairs)).toThrow( | ||
| "Country-currency pairs must be in array format" | ||
| ); | ||
| }); | ||
|
|
||
| Then | ||
| It should return: | ||
| { | ||
| 'US': 'USD', | ||
| 'CA': 'CAD' | ||
| } | ||
| */ | ||
| // Case 5: Throws an error if a country-currency pair is missing a country or currency | ||
| test("throws an error if a country-currency pair is missing a country or currency", () => { | ||
| const countryCurrencyPairs = [ | ||
| ["US", ""], | ||
| ["", "CAD"], | ||
| [" ", "GBP"], | ||
| ]; | ||
|
|
||
| for (const currencyPair of countryCurrencyPairs) { | ||
| expect(() => createLookup([currencyPair])).toThrow( | ||
| "Country and currency codes cannot be empty" | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| // Case 6: Throws an error if a country-currency pair contains more than two elements | ||
| test("throws an error if a country-currency pair contains more than two elements", () => { | ||
| const countryCurrencyPairs = [["GB", "GBP", "ZAR"]]; | ||
|
|
||
| expect(() => createLookup(countryCurrencyPairs)).toThrow( | ||
| "Country-currency pairs must contain exactly two elements: country and currency" | ||
| ); | ||
| }); | ||
|
|
||
| // Case 7: Throws and error if a country-currency pair is duplicated | ||
| test("throws an error if a country-currency pair is duplicated", () => { | ||
| const countryCurrencyPairs = [ | ||
| ["US", "USD"], | ||
| ["CA", "CAD"], | ||
| ["US", "USD"], | ||
| ]; | ||
|
|
||
| expect(() => createLookup(countryCurrencyPairs)).toThrow( | ||
| "Duplicate country code found: US" | ||
| ); | ||
| }); | ||
|
|
||
| // Case 8: Throws an error if non-string values are used as country or currency codes | ||
| test("throws an error if non-string values are used as country or currency codes", () => { | ||
| const countryCurrencyPairs = [ | ||
| [{ name: "United States" }, "USD"], | ||
| ["CA", Infinity], | ||
| ["GB", 1.21], | ||
| [undefined, "ZAR"], | ||
| ["NG", null], | ||
| ]; | ||
|
|
||
| for (const currencyPair of countryCurrencyPairs) { | ||
| expect(() => createLookup([currencyPair])).toThrow( | ||
| "Country-currency pairs must be in string format" | ||
| ); | ||
| } | ||
| }); | ||
| }); |
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.
Good idea!