Repository navigation
London | 26-ITP-Sep | Bartosz Kawiak | Sprint 2 | Sprint2-exercises #1508
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
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,8 @@ | ||
| function contains() {} | ||
| function contains(object, key) { | ||
| if (typeof object !== "object" || Array.isArray(object) || object === null) { | ||
| throw new Error("contains requires an object"); | ||
| } | ||
| return Object.hasOwn(object, key); | ||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,12 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(pairs) { | ||
| const pairsFound = {}; | ||
| for (const pair of pairs) { | ||
| const country = pair[0]; | ||
| const currency = pair[1]; | ||
|
|
||
| pairsFound[country] = currency; | ||
| } | ||
| return pairsFound; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,16 @@ | ||
| function tally() {} | ||
| function tally(array) { | ||
| if (!Array.isArray(array)) { | ||
| throw new Error("tally requires an array"); | ||
| } | ||
| const unique = {}; | ||
| for (const arr of array) { | ||
| if (Object.hasOwn(unique, arr)) { | ||
| unique[arr] += 1; | ||
| } else { | ||
| unique[arr] = 1; | ||
| } | ||
| } | ||
| return unique; | ||
| } | ||
|
|
||
| module.exports = tally; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,23 +7,52 @@ | |
| // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} | ||
|
|
||
| function invert(obj) { | ||
| if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { | ||
| throw new Error("input has to be an object"); | ||
| } | ||
| const invertedObj = {}; | ||
|
|
||
| for (const [key, value] of Object.entries(obj)) { | ||
| invertedObj.key = value; | ||
| invertedObj[value] = key; | ||
| } | ||
|
|
||
| return invertedObj; | ||
| } | ||
|
|
||
| console.log(invert({ a: 1, b: 2 })); | ||
| // 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} | ||
| //{1:a, 2:b} | ||
|
|
||
| // c) What does Object.entries return? Why is it needed in this program? | ||
| //Object.entries() returns an array of key-value pairs from an object. | ||
| //We need it in this program to loop through the object's properties and access both the keys and values, so we can swap them. | ||
|
|
||
| // d) Explain why the current return value is different from the target output | ||
| //The current output is different because dot notation creates a property called "key" instead of using the variable. | ||
| //Each iteration overwrites the previous value, and the original keys and values are not swapped. | ||
|
|
||
| // e) Fix the implementation of invert (and write tests to prove it's fixed!) | ||
| //fixed, should I make a new file with tests or just add them below as I did? | ||
|
|
||
| test("when given single input should invert key and value eg. 'a:1', return '1:a' ", () => { | ||
| expect(invert({ a: 1 })).toEqual({ 1: "a" }); | ||
| }); | ||
|
|
||
| test("when given multiple inputs should invert key and values correctly ", () => { | ||
| expect(invert({ a: 1, b: 3 })).toEqual({ 1: "a", 3: "b" }); | ||
| }); | ||
|
Comment on lines
+41
to
+48
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. In this exercise, you only need to interpret the code. Your answers to the questions are good.
Tests are optional and you could leave the test code here if you want.
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. Thanks for explaining CJ. I'll keep the tests in a single file for better access. |
||
|
|
||
| test("when given input type of number, should throw error", () => { | ||
| expect(() => invert(22)).toThrow(/^input has to be an object$/); | ||
| }); | ||
| test("when given input type of string, should throw error", () => { | ||
| expect(() => invert("22")).toThrow(/^input has to be an object$/); | ||
| }); | ||
| test("when given input with different types of than object, should throw error", () => { | ||
| expect(() => invert([22, 33, "ppp"])).toThrow(/^input has to be an object$/); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.