-
-
Notifications
You must be signed in to change notification settings - Fork 327
London | 26-ITP-May | Dagim Daniel | Sprint 2 | All Section Exercises #1362
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
2e6d362
1cb25ad
db8e129
33b2069
fd5c9ca
230981b
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,18 @@ | ||
| function contains() {} | ||
|
|
||
| function contains(keyInput, valueInput) { | ||
| if ( | ||
| keyInput == null || | ||
| typeof keyInput !== "object" || | ||
| Array.isArray(keyInput) | ||
| ) { | ||
| return false; | ||
| } else { | ||
| for (const key in keyInput) { | ||
| if (key == valueInput) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| // make values like null, undefined ,1234, true,"Abc" to be consider invalid and return false same as array | ||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,28 @@ | ||
| 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 | ||
| test("Give a contains with object and property and if the object contains the property returns true", () => { | ||
| expect(contains({ a: 1, b: 2 }, "b")).toEqual(true); | ||
| }); | ||
|
|
||
| test("Give a contains with empty object and when passed to contains it returns false", () => { | ||
| expect(contains({}, "y")).toEqual(false); | ||
| }); | ||
|
|
||
| test("Give a contains with object and property and if the property is non-existent returns false", () => { | ||
| expect(contains({ a: 1, b: 2 }, "z")).toEqual(false); | ||
| }); | ||
|
|
||
| test("Give invalid parameter in this case array when passed to contains it returns false", () => { | ||
| expect(contains(["a", 2, "b", 3, 2], 2)).toEqual(false); | ||
| }); | ||
|
|
||
| test("Give invalid parameter in this case empty when passed to contains it returns false", () => { | ||
| expect(contains(null)).toEqual(false); | ||
| }); | ||
| test("returns false for undefined", () => { | ||
| expect(contains(undefined, "key")).toEqual(false); | ||
| }); | ||
|
|
||
| test("returns false for numbers", () => { | ||
| expect(contains(1234, "key")).toEqual(false); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(countryCurrencyPairs) { | ||
| pairs = {}; | ||
| for (const [country, currency] of countryCurrencyPairs) { | ||
| pairs[country] = currency; | ||
| } | ||
| return pairs; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,17 @@ | ||
| 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' | ||
| } | ||
| */ | ||
| test("create a country currency code lookup for a single codes", () => { | ||
| expect(createLookup([["UK", "GBP"]])).toEqual({ UK: "GBP" }); | ||
| }); | ||
| test("creates an empty country currency code returns empty lookup", () => { | ||
| expect(createLookup([])).toEqual({}); | ||
| }); | ||
| test("creates a country currency code lookup for multiple codes", () => { | ||
| expect( | ||
| createLookup([ | ||
| ["US", "USD"], | ||
| ["CA", "CAD"], | ||
| ["UK", "GBP"], | ||
| ]) | ||
| ).toEqual({ US: "USD", CA: "CAD", UK: "GBP" }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,12 @@ | ||
| function tally() {} | ||
|
|
||
| function tally(items) { | ||
| if (!Array.isArray(items)) { | ||
| throw new Error("Invalid input!"); | ||
| } | ||
| const result = {}; | ||
| for (const item of items) { | ||
| const count = Object.hasOwn(result, item) ? result[item] : 0; | ||
| result[item] = count + 1; | ||
| } | ||
|
Comment on lines
+5
to
+9
Contributor
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. Does the following function call returns the value you expect? Suggestion:
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. it function as expected let say console.log (tally(["a","a","b","a","c"]) ==> {a:3, b:1,c:1}
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. may be i don't understand your questions, could you explain your question a little bit sorry
Contributor
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. Have you tried calling the function with this exact argument? |
||
| return result; | ||
| } | ||
| module.exports = tally; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,22 @@ | ||
| 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 } | ||
| */ | ||
| test("given an array of items an object containing the counter for each item", () => { | ||
| expect(tally(["a", "a"])).toEqual({ a: 2 }); | ||
| }); | ||
|
|
||
| // Acceptance criteria: | ||
| test("tally on an empty array returns an empty object", () => { | ||
| expect(tally([])).toEqual({}); | ||
| }); | ||
|
|
||
| // 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("given an array of items an object containing the counter for each item", () => { | ||
| expect(tally(["a", "a", "c", "b", "b", "d"])).toEqual({ | ||
| a: 2, | ||
| c: 1, | ||
| b: 2, | ||
| d: 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 on an empty array returns an empty object", () => { | ||
| expect(() => tally("string")).toThrow("Invalid input!"); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,30 @@ | ||
| // Let's define how invert should work | ||
|
|
||
| // Given an object | ||
| // When invert is passed this object | ||
| // Then it should swap the keys and values in the object | ||
|
|
||
| // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} | ||
|
|
||
| function invert(obj) { | ||
| const invertedObj = {}; | ||
|
|
||
| for (const [key, value] of Object.entries(obj)) { | ||
| invertedObj.key = value; | ||
| //invertedObj.key = value; | ||
| invertedObj[value] = key; | ||
| } | ||
|
|
||
| return invertedObj; | ||
| } | ||
|
|
||
| // a) What is the current return value when invert is called with { a : 1 } | ||
|
|
||
| /* | ||
| { | ||
| key: 1; | ||
| }*/ | ||
| // b) What is the current return value when invert is called with { a: 1, b: 2 } | ||
|
|
||
| /* | ||
| { | ||
| key: 2; | ||
| } | ||
| */ | ||
| // c) What is the target return value when invert is called with {a : 1, b: 2} | ||
|
|
||
| // c) What does Object.entries return? Why is it needed in this program? | ||
|
|
||
| // d) Explain why the current return value is different from the target output | ||
|
|
||
| // e) Fix the implementation of invert (and write tests to prove it's fixed!) | ||
| // {"1":"a", "2":"b"} | ||
| // d) What does Object.entries return? Why is it needed in this program? | ||
| //object.entries takes an objecet and returns an array of its key-value pairs. | ||
| // e) Explain why the current return value is different from the target output | ||
| //because we use .key notation we are setting a property named "key" on our Object. | ||
| // f) Fix the implementation of invert (and write tests to prove it's fixed!) | ||
| module.exports = invert; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| const invert = require("./invert.js"); | ||
|
|
||
| test("swap key with value", () => { | ||
| expect(invert({ a: 1 })).toEqual({ 1: "a" }); | ||
| }); | ||
|
|
||
| test("swap key with value", () => { | ||
| expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" }); | ||
| }); |
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.
Should values like
null,undefined,1234,true,"ABC"be considered as invalid first parameter as well?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.
// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error this were the question that's why i make it false.
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.
That means "array" is just one possible kind of invalid parameters. There could be others.