-
-
Notifications
You must be signed in to change notification settings - Fork 327
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?
Changes from all commits
6a0643a
6de1246
2488a71
0291dfa
bced4fe
5866558
3423b50
2a62f58
0b6fc65
9c07c98
95e7d98
4de66ef
078648c
6d32545
1e8685b
c0107ff
a6c0620
0b68a26
4a89c7d
8ef6662
0ac7c8a
f7b2d6f
c83a6e2
cbed42f
c02c23e
b46cc1c
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,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); | ||
|
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. Nice choice of method
Author
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. thank you i came across it when learning about iterating through objects. very handy. |
||
| } | ||
|
|
||
| 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", | ||
| }); | ||
| }); | ||
| }); | ||
|
Comment on lines
+26
to
+43
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 you have included a simpler test to start with for valid inputs?
Author
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. i added a simpler input with only one array in the array (one country's data) |
||
|
|
||
| /* | ||
|
|
||
|
|
@@ -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", () => { | ||
| test("should return an object with counts for each unique item", () => { | ||
| expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 }); | ||
|
Comment on lines
+28
to
+38
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. I actually think these are three different behaviours and so deserve their own test blocks to anyone reading them test suite can see the different behaviours clearly.
Author
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. corrected. |
||
| }); | ||
| }); | ||
| // 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"); | ||
| }); | ||
| }); | ||
| }); | ||
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