-
-
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?
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,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 @@ | ||
| function createLookup() { | ||
| function createLookup(arr) { | ||
| // implementation here | ||
| const obj = Object.fromEntries(arr); | ||
| return obj; | ||
| } | ||
|
|
||
| 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; | ||
| }, {}); | ||
|
Comment on lines
+2
to
+10
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. Does the following function call returns the value you expect? Suggestion:
Author
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. Thanks! I tested this and found that toString is inherited from Object.prototype so my original condition could treat it as if it already existed in the tally object. I can use Object.hasOwn() to check whether the key is actually an own property of the result object. |
||
|
|
||
| return result; | ||
| } | ||
|
|
||
| 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.
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().