Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 326
London | 26-ITP-May | Edina Kurdi | Sprint 2 | Coursework#1422
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?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
6a0643a6de12462488a710291dfabced4fe58665583423b502a62f580b6fc659c07c9895e7d984de66ef078648c6d325451e8685bc0107ffa6c06200b68a264a89c7d8ef66620ac7c8af7b2d6fc83a6e2cbed42fc02c23eb46cc1cFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,20 @@ | ||
| function contains() {} | ||
| function contains(object, propertyName) { | ||
| if (typeof object !== "object" || object === null || Array.isArray(object)) { | ||
| throw new Error("Input should be an object"); | ||
| } | ||
| const keysInObject = Object.keys(object); | ||
| return keysInObject.includes(propertyName); | ||
| } | ||
| module.exports = contains; | ||
| /* | ||
| 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' | ||
| */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,52 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(countryCurrencyPairs) { | ||
| //if countryCurrencyPairs is not an array, throw error | ||
| if (!Array.isArray(countryCurrencyPairs)) { | ||
| throw new Error("Invalid input. It should be an array"); | ||
| } | ||
| //if it is an empty array, throw error | ||
| if (countryCurrencyPairs.length === 0) { | ||
| throw new Error("Input should not be an empty array"); | ||
| } | ||
| //if not all elements are an array in the array, throw an error | ||
| if (!countryCurrencyPairs.every(Array.isArray)) { | ||
| throw new Error("Invalid input. All elements should be arrays"); | ||
| } | ||
| return Object.fromEntries(countryCurrencyPairs); | ||
Poonam-raj marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| module.exports = createLookup; | ||
| // console.log( | ||
| // createLookup([ | ||
| // ["US", "USD"], | ||
| // ["CA", "CAD"], | ||
| // ]) | ||
| // ); | ||
| // console.log(createLookup([])); | ||
| /* | ||
| 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' | ||
| } | ||
| */ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,46 @@ | ||
| const createLookup = require("./lookup.js"); | ||
| test.todo("creates a country currency code lookup for multiple codes"); | ||
| describe("when given invalid inputs", () => { | ||
| test("should return invalid input error, if the input is an empty array", () => { | ||
| expect(() => createLookup([])).toThrow( | ||
| "Input should not be an empty array" | ||
| ); | ||
| }); | ||
| test("should return invalid input error, if the input isn't an array of arrays", () => { | ||
| expect(() => createLookup(["hi", "hello"])).toThrow( | ||
| "Invalid input. All elements should be arrays" | ||
| ); | ||
| }); | ||
| test("should return invalid input error, if the input isn't an array of arrays", () => { | ||
| expect(() => createLookup("hi")).toThrow( | ||
| "Invalid input. It should be an array" | ||
| ); | ||
| }); | ||
| test("should return invalid input error, if the input isn't an array of arrays", () => { | ||
| expect(() => createLookup(2)).toThrow( | ||
| "Invalid input. It should be an array" | ||
| ); | ||
| }); | ||
| }); | ||
| describe("when given valid inputs", () => { | ||
| test("should return an object where (Input ==> Output): keys:values ==> country code: corresponding currency", () => { | ||
| expect(createLookup([["US", "USD"]])).toEqual({ | ||
| US: "USD", | ||
| }); | ||
| }); | ||
| test("should return an object where (Input ==> Output): keys:values ==> country code: corresponding currency", () => { | ||
| expect( | ||
| createLookup([ | ||
| ["US", "USD"], | ||
| ["CA", "CAD"], | ||
| ]) | ||
| ).toEqual({ | ||
| US: "USD", | ||
| CA: "CAD", | ||
| }); | ||
| }); | ||
| }); | ||
Poonam-raj marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| /* | ||
| @@ -9,7 +49,7 @@ 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 | ||
| - An array of arrays representing when given invalid inputs code pairs | ||
| e.g. [['US', 'USD'], ['CA', 'CAD']] | ||
| When | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,18 @@ | ||
| function tally() {} | ||
| function tally(array) { | ||
| if (!Array.isArray(array)) { | ||
| throw new Error("Input should be an array"); | ||
| } | ||
| const tallySet = {}; | ||
| for (let item of array) { | ||
| if (!tallySet[item]) { | ||
| tallySet[item] = 1; | ||
| } else { | ||
| tallySet[item] += 1; | ||
| } | ||
| } | ||
| return tallySet; | ||
| } | ||
| module.exports = tally; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -19,16 +19,46 @@ 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 | ||
| describe("tally()", () => { | ||
| // Given an array with duplicate items | ||
| // When passed to tally | ||
| // Then it should return counts for each unique item | ||
| describe("when given an array with a single item", () => { | ||
| test("should return an object with counts for that item (1)", () => { | ||
| expect(tally(["a"])).toEqual({ a: 1 }); | ||
| }); | ||
| }); | ||
| describe("when given an array with duplicate items", () => { | ||
| test("should return an object with counts that item", () => { | ||
| expect(tally(["a", "a", "a"])).toEqual({ a: 3 }); | ||
| }); | ||
| }); | ||
| describe("when given an array with duplicate items", () => { | ||
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. Avoid having two describe blocks with the same text (this is the same as line 31) | ||
| test("should return an object with counts for each unique item", () => { | ||
| expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 }); | ||
Poonam-raj marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }); | ||
| }); | ||
| // Given an empty array | ||
| // When passed to tally | ||
| // Then it should return an empty object | ||
| describe("when given an empty array", () => { | ||
| test("should return an empty object", () => { | ||
| expect(tally([])).toEqual({}); | ||
| }); | ||
| }); | ||
| // 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 | ||
| // Given an invalid input like a string | ||
| // When passed to tally | ||
| // Then it should throw an error | ||
| describe("when given invalid input", () => { | ||
| test("should throw an error for a string input", () => { | ||
| expect(() => tally("apple")).toThrow("Input should be an array"); | ||
| }); | ||
| test("should throw an error for a boolean input", () => { | ||
| expect(() => tally(true)).toThrow("Input should be an array"); | ||
| }); | ||
| test("should throw an error for a number input", () => { | ||
| expect(() => tally(3)).toThrow("Input should be an array"); | ||
| }); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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.
A good workaround to print the values of the object.
Question - how could the for loop on lines 21 - 23 be changed (to a different loop for example) to achieve the same effect?
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.
Created an alternative using a loop
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 refactor- I was also thinking of a
for inloop, if we changed line 21 to afor inyou'd have a direct way of printing the values in a readable way