Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 327
West-Midlands | 26-ITP-May | Maryam Janjua | Sprint 2 | Sprint 2 Exercises#1433
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?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,20 @@ | ||
| // Predict and explain first... | ||
| /* | ||
| author is an object. The original code tries to use a for...of loop directly on the object, but normal objects | ||
| cannot be directly iterated using for...of. Since we only want the property values, we can use Object.values(author) | ||
| to get all the values from the object. | ||
| */ | ||
| // This program attempts to log out all the property values in the object. | ||
| // But it isn't working. Explain why first and then fix the problem | ||
| const author = { | ||
| firstName: "Zadie", | ||
| lastName: "Smith", | ||
| occupation: "writer", | ||
| age: 40, | ||
| alive: true, | ||
| }; | ||
| console.log(Object.values(author)); | ||
| for (const value of author) { | ||
| console.log(value); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,14 @@ | ||
| function contains() {} | ||
| function contains(obj, item) { | ||
| if (typeof obj !== "object" || obj === null || Array.isArray(obj)){ | ||
| return false; | ||
| } | ||
| let getKey = Object.keys(obj); | ||
| for (let element of getKey){ | ||
| if(element === item){ | ||
| return true | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| module.exports = contains; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| functioncreateLookup(){ | ||
| functioncreateLookup(arr){ | ||
| // implementation here | ||
| constobj=Object.fromEntries(arr); | ||
| returnobj; | ||
| } | ||
| module.exports=createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,48 @@ | ||
| function parseQueryString(queryString) { | ||
| const queryParams = {}; | ||
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
| const keyValuePairs = queryString.split("&"); | ||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| if (pair === "") { | ||
| continue; | ||
| } | ||
| const checkSeparator = pair.indexOf("="); | ||
| let key; | ||
| let value; | ||
| if (checkSeparator === -1) { | ||
| key = pair; | ||
| value = ""; | ||
| } else { | ||
| key = pair.slice(0, checkSeparator); | ||
| value = pair.slice(checkSeparator + 1); | ||
| } | ||
| key = key.replace(/\+/g, " "); | ||
| value = value.replace(/\+/g, " "); | ||
| key = decodeURIComponent(key); | ||
| value = decodeURIComponent(value); | ||
| if (Object.prototype.hasOwnProperty.call(queryParams, key)) { | ||
Contributor 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. Could also use | ||
| if (Array.isArray(queryParams[key])) { | ||
| queryParams[key].push(value); | ||
| } else { | ||
| queryParams[key] = [queryParams[key], value]; | ||
| } | ||
| } else { | ||
| queryParams[key] = value; | ||
| } | ||
| } | ||
| return queryParams; | ||
| } | ||
| module.exports = parseQueryString; | ||
| module.exports = parseQueryString; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,15 @@ | ||
| function tally() {} | ||
| function tally(arr) { | ||
| const result = arr.reduce((obj, item) => { | ||
| if (Object.hasOwn(obj, item)) { | ||
| obj[item] = obj[item] + 1; | ||
| } else { | ||
| obj[item] = 1; | ||
| } | ||
| module.exports = tally; | ||
| return obj; | ||
| }, {}); | ||
cjyuan marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return result; | ||
| } | ||
| module.exports = tally; | ||
Uh oh!
There was an error while loading. Please reload this page.
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 works.
Do check out
Object.hasOwn()and alsouse AI to find out the trade-off among different ways to check if an object contains a particular key.
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.
Thanks! I checked Object.hasOwn(). I could simplify my function by using Object.hasOwn(obj, item) instead of getting all the keys and looping through them. It also checks only the object's own properties, which fits this function well. I'll also look into the trade-offs between Object.hasOwn(), in, and hasOwnProperty().