-
-
Notifications
You must be signed in to change notification settings - Fork 149
CYF London | Fatma Arslantas | Module-Data-Groups | Week 2 #199
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 3 commits
9edc25b
cd49bda
4674171
9af05a7
874a46d
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,19 @@ | ||
function contains() {} | ||
function contains(obj, key) { | ||
// 〰️ Checking if the input is a valid object or not null or an array | ||
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { | ||
return false; | ||
} | ||
// 〰️ Using hasOwnProperty to check if the key exists directly on the object | ||
return obj.hasOwnProperty(key); | ||
} | ||
|
||
console.log(contains({a: 1, b: 2}, 'a')); | ||
console.log(contains({ name: "Ali", age: 25 }, 'age')); | ||
console.log(contains({ x: 10, y: 20 }, 'y')); | ||
|
||
console.log(contains({a: 1, b: 2}, 'c')); | ||
console.log(contains({}, 'a')); | ||
|
||
|
||
|
||
module.exports = contains; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,22 @@ | ||
function createLookup() { | ||
// implementation here | ||
// 〰️ Function to create a lookup object from an array of country-currency pairs | ||
function createLookup(countryCurrencyPairs) { | ||
// 〰️ Use Object.fromEntries to transform the array into an object | ||
return Object.fromEntries(countryCurrencyPairs); | ||
} | ||
|
||
console.log(createLookup([['US', 'USD'], ['CA', 'CAD']])); | ||
console.log(createLookup([['IN', 'INR'], ['JP', 'JPY']])); | ||
console.log(createLookup([])); | ||
|
||
module.exports = createLookup; | ||
|
||
|
||
// 〰️ Explanation of Object.fromEntries(): | ||
// 〰️ Object.fromEntries() is a built-in JavaScript method that converts an array of key-value pairs into an object. It's particularly useful when you have data in array format and want to convert it into an object without using loops or more complex methods like reduce(). | ||
|
||
// 〰️ How It Works: | ||
// 〰️ 1. It expects an array where each element is a two-item array representing [key, value]. | ||
// 〰️ 2.It processes each pair and adds the key-value pair to a new object. | ||
// 〰️ 3.It returns the resulting object. | ||
|
||
// 〰️ Since createLookup takes an array of pairs, using Object.fromEntries() makes the conversion simple and clean. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,45 @@ | ||
function parseQueryString(queryString) { | ||
const queryParams = {}; | ||
if (queryString.length === 0) { | ||
|
||
// 〰️ Check for null, undefined, or non-string input | ||
if (!queryString || typeof queryString !== "string") { | ||
return queryParams; | ||
} | ||
|
||
// 〰️ Split the query string into key-value pairs using '&' | ||
const keyValuePairs = queryString.split("&"); | ||
|
||
for (const pair of keyValuePairs) { | ||
const [key, value] = pair.split("="); | ||
// 〰️ Split each pair by the first '=' and keep remaining as the value | ||
const [rawKey, ...rawValue] = pair.split("="); | ||
|
||
// 〰️ Decode keys and values safely | ||
// 〰️ Using decodeURIComponent() to convert percent-encoded strings into readable text. | ||
// 〰️ Example: 'John%20Doe' -> 'John Doe', '5%25' -> '5%' | ||
// 〰️ rawKey || "" ensures that if rawKey is undefined, null, or empty, it returns to an empty string. | ||
const key = decodeURIComponent(rawKey || ""); | ||
const value = decodeURIComponent(rawValue.join("=") || ""); | ||
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. you demonstrates a solid understanding of value-key pair parsing. Maybe in order to enhance test cases, you can also add dublicate test case for keys i.e 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 feedback @halilibrahimcelik! I've updated the implementation to handle duplicate keys by storing their values in an array. Additionally, I’ve added a test case to validate this behavior, ensuring that query strings like key=value1&key=value2 are correctly parsed into { key: ["value1", "value2"] }. I hadn’t considered this, but your suggestion helped improve my code. Thanks again! Let me know if there’s anything else I can improve. 🙌 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. I'm glad to hear that! It’s a niche case, and in a real-world scenario, it’s unlikely to have two different values with the same key. However, it’s a good challenge, keep up the good work! Cheers 🎉 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! 🤗 |
||
|
||
// 〰️ Store the key-value pair in the object | ||
queryParams[key] = value; | ||
} | ||
|
||
return queryParams; | ||
} | ||
|
||
console.log(parseQueryString("equation=x=y+1")); | ||
console.log(parseQueryString("key1=value1&key2=value2")); | ||
console.log(parseQueryString("equation=x=y+1&other=abc=123")); | ||
console.log(parseQueryString("name=John%20Doe&value=5%25")); | ||
console.log(parseQueryString("first%20name=Alice")); | ||
console.log(parseQueryString("%26key%3D=value%3F")); | ||
console.log(parseQueryString("equation=x%3Dy%2B1")); | ||
console.log(parseQueryString("specialChars=!%40%23%24%25%5E%26*()")); | ||
console.log(parseQueryString("emptyKey=")); | ||
console.log(parseQueryString("=emptyValue")); | ||
console.log(parseQueryString("multiple=values&key=value")); | ||
console.log(parseQueryString(null)); | ||
console.log(parseQueryString(undefined)); | ||
console.log(parseQueryString("")); | ||
|
||
module.exports = parseQueryString; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,28 @@ | ||
function tally() {} | ||
function tally(arr) { | ||
// 〰️ Check if input is an array | ||
if (!Array.isArray(arr)) { | ||
throw new Error("Input must be an array") | ||
} | ||
|
||
// 〰️ Create an empty object to store counts | ||
let counts = {}; | ||
|
||
// 〰️ Loop through the array and count items | ||
for (let item of arr) { | ||
if (counts[item]) { | ||
counts[item] += 1; // 〰️ Increase count if item exists | ||
} else { | ||
counts[item] = 1; // 〰️ Initialize count if item is new | ||
} | ||
} | ||
|
||
return counts; // 〰️ Return the final counts object | ||
} | ||
|
||
console.log(tally(['a'])); | ||
console.log(tally(['a', 'a', 'a'])); | ||
console.log(tally(['a', 'a', 'b', 'c'])); | ||
console.log(tally([])); | ||
// console.log(tally('a')); | ||
|
||
module.exports = tally; |
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.
Great job verifying the object parameter types in the contains function, and you've successfully met all the task requirements!
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.
Hi @halilibrahimcelik,
Thank you for the feedback! I’m glad to hear that. Let me know if there’s anything else I can improve. 🤗