-
-
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; | ||
| } | ||
|
Comment on lines
+5
to
+12
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. This works. Do check out |
||
|
|
||
| module.exports = contains; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,25 +11,37 @@ E.g. contains({a: 1, b: 2}, 'c') // returns false | |
| as the object doesn't contains a key of 'c' | ||
| */ | ||
|
|
||
| // Acceptance criteria: | ||
|
|
||
| // 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("return true if object's property exists otherwise false", () => { | ||
| expect(contains({name: "maryam", city: "Derby"}, "city")).toBe(true); | ||
| }); | ||
|
|
||
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| test.todo("contains on empty object returns false"); | ||
| test("return false if object is empty", () => { | ||
| expect(contains({}, "a")).toBe(false); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
| test("return true if object's property contains the property", () => { | ||
| expect(contains({name: "maryam", city: "Derby"}, "city")).toBe(true); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| test("return false if property is non-existent", () => { | ||
| expect(contains({name: "maryam", city: "Derby"}, "age")).toBe(false); | ||
| }); | ||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("return false if it's not an object", () => { | ||
| expect(contains([], 6)).toBe(false); | ||
| }); | ||
|
Comment on lines
+45
to
+47
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. When a function does not test if the first argument is an array, A proper test should use a non-empty array along with a valid |
||
| 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,14 @@ | ||
| function tally() {} | ||
|
|
||
| function tally(arr) { | ||
| const result = arr.reduce((obj, item) => { | ||
| if (obj [item]) { | ||
| obj[item] = obj[item] + 1; | ||
| } | ||
| else{ | ||
| obj[item] = 1; | ||
| } | ||
| 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:
|
||
|
|
||
| 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.
Is it necessary to use
Object.values()on line 18?If we do not need to reassign a value to the loop variable, common practice is to declare it using
const.