-
-
Notifications
You must be signed in to change notification settings - Fork 94
ITP -Northwest- JAN 2025 | Jan Lo | Module-Data groups | SPRINT 2 | WEEK 7 #506
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,24 @@ | ||
function contains() {} | ||
function contains(element, key) { | ||
const result = element[key]; | ||
|
||
// console.log({ | ||
// key, | ||
// typeKey: typeof key, | ||
|
||
// result, | ||
// typeResult: typeof result | ||
// }) | ||
|
||
// if undefined | ||
// return false as doesnt exist | ||
// else, does exist! | ||
// return true | ||
|
||
if (result === undefined) return false; | ||
else return true; | ||
|
||
} | ||
console.log(contains({a: 1, b: 2}, 'a')) | ||
console.log(contains({a: 1, b: 2}, 'c')) | ||
console.log(contains({firstName: "Cam" }, 'firstName')) | ||
module.exports = contains; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ 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 | ||
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 | ||
|
@@ -20,16 +20,26 @@ as the object doesn't contains a key of 'c' | |
// 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({})).toBe (false); | ||
}); | ||
// Given an object with properties | ||
// When passed to contains with an existing property name | ||
// Then it should return true | ||
test("contains with an existing property name", () => { | ||
expect(contains({a: 1, b: 2}, 'a')).toBe (true); | ||
}); | ||
|
||
// Given an object with properties | ||
// When passed to contains with a non-existent property name | ||
// Then it should return false | ||
|
||
test("contains with a non-existent property name", () => { | ||
expect(contains({a: 1, b: 2}, 'c')).toBe (false); | ||
}); | ||
// Given invalid parameters like an array | ||
// When passed to contains | ||
// Then it should return false or throw an error | ||
test("invalid parameters", () => { | ||
expect(contains(["apple", "orange", "pear"])).toBe (false); | ||
}); | ||
|
||
Comment on lines
+42
to
+45
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. Invalid parameters could also be other types of values. For examples, With your current implementation, You would need to add code to your function to check the first parameter is a valid object and is not an array. Note: array is a kind of objects in JS. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,54 @@ | ||
function createLookup() { | ||
// implementation here | ||
// function createLookup(countryCurrencyPairs) { | ||
// for( let i = 0; i < countryCurrencyPairs.length; i++) { | ||
// const countryCurrencyPair = countryCurrencyPairs[i] | ||
|
||
// // console.log({ | ||
// // countryCurrencyPairs, | ||
// // i, | ||
// // value: countryCurrencyPairs[i], | ||
// // countryCurrencyPair, | ||
|
||
// // }) | ||
|
||
// //console.log('i am here:', countryCurrencyPairs[[i]]) | ||
// console.log(countryCurrencyPair[]) | ||
|
||
// } | ||
|
||
// return countryCurrencyPairs; | ||
|
||
// const country = countryCurrencyPair[0] | ||
// const currency = countryCurrencyPair[1] | ||
|
||
// return{[country]:currency}; | ||
|
||
|
||
// // implementation here | ||
// } | ||
|
||
// function createSingleLookup(countryCurrencyPair) { | ||
// const country = countryCurrencyPair[0] | ||
// const currency = countryCurrencyPair[1] | ||
|
||
// return{[country]:currency}; | ||
// } | ||
|
||
// console.log(createLookup([['US', 'USD'], ['CA', 'CAD']])) | ||
|
||
// console.log(createSingleLookup(['US', 'USD'])) // return { 'US': 'USD' } | ||
|
||
function createLookup(countryCurrencyPairs) { | ||
return countryCurrencyPairs.reduce((lookup, pair) => { | ||
const [country, currency] = pair; | ||
lookup[country] = currency; | ||
return lookup; | ||
}, {}); | ||
} | ||
|
||
module.exports = createLookup; | ||
|
||
|
||
// for (let i = 0; i < onlyNumbers.length; i++) { | ||
// total += onlyNumbers[i]; | ||
// } | ||
// return total; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,8 @@ function parseQueryString(queryString) { | |
const keyValuePairs = queryString.split("&"); | ||
|
||
for (const pair of keyValuePairs) { | ||
const [key, value] = pair.split("="); | ||
const [key, ...valueParts] = pair.split("="); | ||
const value = valueParts.join("="); | ||
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. Your approach works. Please note that in real querystring, both |
||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
function tally() {} | ||
|
||
module.exports = tally; | ||
function tally(items) { | ||
if (!Array.isArray(items)) { | ||
throw new Error("Input must be an array"); | ||
} | ||
|
||
return items.reduce((counts, item) => { | ||
counts[item] = (counts[item] || 0) + 1; | ||
return counts; | ||
}, {}); | ||
} | ||
|
||
module.exports = tally; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const invert = require("./invert.js"); | ||
|
||
test("inverts an object with one key-value pair", () => { | ||
expect(invert({ a: 1 })).toEqual({ "1": "a" }); | ||
}); | ||
|
||
test("inverts an object with multiple key-value pairs", () => { | ||
expect(invert({ a: 1, b: 2 })).toEqual({ "1": "a", "2": "b" }); | ||
}); | ||
|
||
test("handles an empty object", () => { | ||
expect(invert({})).toEqual({}); | ||
}); | ||
|
||
test("handles objects with numeric keys and string values", () => { | ||
expect(invert({ 1: "a", 2: "b" })).toEqual({ a: "1", b: "2" }); | ||
}); |
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 approach is not bulletproof because a property can be assigned an
undefined
value. For example,Can you figure out how else to improve the code?