-
-
Notifications
You must be signed in to change notification settings - Fork 93
ITP-Jan | NW | Jovy So | Module-Data-Groups | WEEK 2 #508
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,16 @@ | ||
function contains() {} | ||
function contains(object, propertyName) { | ||
// console.log(propertyName, '<-- property name'); | ||
// if (!propertyName) return false; | ||
// if (!object) return false; | ||
if (typeof object !== "object" || object === null || Array.isArray(object) || typeof object === "string") return false; | ||
if (object.hasOwnProperty(propertyName)) return true; | ||
else return false; | ||
}; | ||
|
||
module.exports = contains; | ||
|
||
console.log(contains({a: 1, b: 2}, 'a')); | ||
console.log(contains({a: 1, b: 2})); | ||
console.log(contains()); | ||
console.log(contains({a: 1, b: 2}, 'c')); | ||
console.log(contains([1,2,3])); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
function createLookup() { | ||
function createLookup(countryCurrencyPairs) { | ||
// implementation here | ||
} | ||
if (countryCurrencyPairs !== undefined) return Object.fromEntries(countryCurrencyPairs); | ||
}; | ||
|
||
module.exports = createLookup; | ||
|
||
console.log(createLookup([['US', 'USD'], ['CA', 'CAD'], ["HK", 'HKD']])); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ test("parses querystring values containing =", () => { | |
"equation": "x=y+1", | ||
}); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
function tally() {} | ||
function tally(items) { | ||
let countLetter = {}; | ||
if (Array.isArray(items) === false) throw new Error("You must provide an item"); | ||
else if (items.length === 0) return countLetter; | ||
else { | ||
items.forEach(letter => countLetter[letter] ? countLetter[letter]++ : countLetter[letter] = 1) | ||
return countLetter; | ||
}; | ||
}; | ||
|
||
module.exports = tally; | ||
|
||
console.log(tally(['a', 'a', 'a'])); // output: { a: 3 } | ||
console.log(tally(['a', 'b', 'c'])); // output: { a: 1, b: 1, c: 1 } | ||
console.log(tally([])); // output: {} | ||
// console.log(tally('Hello')); // output: Throw Error "You must provide an item" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,20 +10,34 @@ function invert(obj) { | |
const invertedObj = {}; | ||
|
||
for (const [key, value] of Object.entries(obj)) { | ||
invertedObj.key = value; | ||
invertedObj[value] = key; | ||
} | ||
|
||
return invertedObj; | ||
} | ||
|
||
module.exports = invert; | ||
|
||
console.log(invert({x : 10, y : 20})); //output: {"10": "x", "20": "y"} | ||
console.log(invert({ a: 1, b: 2 })); // output: {"1": "a", "2": "b"} | ||
|
||
// 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 the key/pairs of an object, it makes it simple to use objects in loops. | ||
// In this case, if Object.entries is removed, it would occur error showing obj is not iterable. | ||
// Therefore, Object.entries is needed as it changed obj to array so that it can be looped over. | ||
|
||
// d) Explain why the current return value is different from the target output | ||
// There are two problems in the current function | ||
// 1. key and value haven't been swap | ||
// 2. invertedObj.key is unable to return the key of the object | ||
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. The statement 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. Of course, let me try to explain after asking ChatGPT. |
||
|
||
// e) Fix the implementation of invert (and write tests to prove it's fixed!) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const invert = require("./invert.js"); | ||
|
||
test("Given an object, then swap the keys and values in the object", () => { | ||
expect(invert({x : 10, y : 20})).toEqual({"10": "x", "20": "y"}); | ||
}); |
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.
This check is,
typeof object === "string"
, is not needed because a string is not an object; the first condition would already be true.