-
-
Notifications
You must be signed in to change notification settings - Fork 94
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 1 commit
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,15 @@ | ||
function contains() {} | ||
function contains(object, propertyName) { | ||
// console.log(propertyName, '<-- property name'); | ||
if (!propertyName) return false; | ||
if (!object) return false; | ||
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. This two statements would not return false when |
||
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 |
---|---|---|
|
@@ -16,20 +16,38 @@ as the object doesn't contains a key of 'c' | |
// 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 | ||
test("given an object and property arg returns true", () => { | ||
expect(contains({a: 1, b: 2}, 'a')).toEqual(true); | ||
}); | ||
|
||
test("given an object, but no property arg then returns false", () => { | ||
expect(contains({a: 1, b: 2})).toEqual(false); | ||
}); | ||
|
||
// Given an empty object | ||
// When passed to contains | ||
// Then it should return false | ||
test.todo("contains on empty object returns false"); | ||
test("contains on empty object returns false", () => { | ||
expect(contains()).toEqual(false); | ||
jovyso marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
// Given an object with properties | ||
// When passed to contains with an existing property name | ||
// Then it should return true | ||
test("contains the property in the object returns true", () => { | ||
expect(contains({a: 1, b: 2}, 'a')).toEqual(true); | ||
}); | ||
jovyso marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Given an object with properties | ||
// When passed to contains with a non-existent property name | ||
// Then it should return false | ||
test("not contains the property in the object returns false", () => { | ||
expect(contains({a: 1, b: 2}, 'c')).toEqual(false); | ||
}); | ||
|
||
// Given invalid parameters like an array | ||
// When passed to contains | ||
// Then it should return false or throw an error | ||
test("contains invalid parameters return false", () => { | ||
expect(contains([1,2,3])).toEqual(false); | ||
}); | ||
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. Array is a kind of objects in JS, where its keys are its indexes. To properly reject parameters that is one of these
you have to "improve" your check in 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 for you comments! I have improved the function and the test in |
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 |
---|---|---|
|
@@ -6,11 +6,13 @@ function parseQueryString(queryString) { | |
const keyValuePairs = queryString.split("&"); | ||
|
||
for (const pair of keyValuePairs) { | ||
const [key, value] = pair.split("="); | ||
const [key, value] = pair.split(/=(.*)/s); | ||
queryParams[key] = value; | ||
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. Interesting approach. Please note that in real querystring, both 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 for your suggestion. I have found some results and tried to improve the function and I am gonna explore more. |
||
} | ||
|
||
return queryParams; | ||
} | ||
|
||
module.exports = parseQueryString; | ||
|
||
console.log(parseQueryString("equation=x=y+1")); |
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"}); | ||
}); |
Uh oh!
There was an error while loading. Please reload this page.