Repository navigation
London | 26-ITP-SEPT | Abakar Souleyman | Sprint 2 | Module-Data-Groups #1528
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
aaf5f11
e6e87b7
07df808
d86885d
c29c10b
37baeb8
0a4689e
e1f82e7
a7e2381
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,property) { | ||
| if (typeof object !== "object" || object === null || Array.isArray(object)) { | ||
| throw new Error("contains requires an object"); | ||
| } | ||
|
|
||
| return Object.hasOwn(object, property); | ||
| } | ||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,14 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(countryCurrencyPairs) { | ||
| const c_Lookup = {}; | ||
|
|
||
| for (let i = 0; i < countryCurrencyPairs.length; i++) { | ||
| const country = countryCurrencyPairs[i][0]; | ||
| const currency = countryCurrencyPairs[i][1]; | ||
|
|
||
| c_Lookup[country] = currency; | ||
| } | ||
|
|
||
| return c_Lookup; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,18 @@ | ||
| function tally() {} | ||
| function tally(items) { | ||
| if (!Array.isArray(items)) { | ||
| throw new Error("tally requires an array"); | ||
| } | ||
| const list_of_items= {}; | ||
|
|
||
| for (const item of items) { | ||
| if (list_of_items [item]) { | ||
|
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. What does To "like" or "dislike" this comment, please follow this link |
||
| list_of_items[item]++; | ||
| } else { | ||
| list_of_items[item] = 1; | ||
| } | ||
|
|
||
| } | ||
| return list_of_items; | ||
| } | ||
|
|
||
| module.exports = tally; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,20 +10,23 @@ function invert(obj) { | |
| const invertedObj = {}; | ||
|
|
||
| for (const [key, value] of Object.entries(obj)) { | ||
| invertedObj.key = value; | ||
| invertedObj[value] = key; | ||
| } | ||
|
|
||
| return invertedObj; | ||
| } | ||
|
|
||
| console.log(invert); | ||
|
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. What output appears whenever a test or another module imports this file? To "like" or "dislike" this comment, please follow this link |
||
| // 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 (list) of pairs containing the keys and values from an object. | ||
| // -It's needed so we can access both the key & the value and swap them. | ||
| // d) Explain why the current return value is different from the target output | ||
|
|
||
| // Because invertedObj.key = value always uses the word 'key' as the property name. Instead, we should use invertedObj[value] = key to swap the original key and value. | ||
| // e) 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,8 @@ | ||
| const invert = require("./invert.js"); | ||
|
|
||
| test("should swap keys and values", () => { | ||
| 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.
The fixture uses
GPBforGB, and the expected object repeats that value, so this test accepts the wrong currency code. What code should this mapping use?To "like" or "dislike" this comment, please follow this link