-
-
Notifications
You must be signed in to change notification settings - Fork 271
London | ITP-JAN26 | Ihor Taradaiko | Sprint 2 | Coursework/sprint-2 #1046
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,15 +1,23 @@ | ||
| // Predict and explain first... | ||
| // This will not log the ingredients correctly. It will print the whole | ||
| // recipe object as [object Object] instead of each ingredient on its own line, | ||
| // because you're interpolating the entire object (`${recipe}`) instead of | ||
| // its properties and the ingredients array items. | ||
|
|
||
| // This program should log out the title, how many it serves and the ingredients. | ||
| // Each ingredient should be logged on a new line | ||
| // How can you fix it? | ||
|
|
||
|
|
||
| const recipe = { | ||
| title: "bruschetta", | ||
| serves: 2, | ||
| ingredients: ["olive oil", "tomatoes", "salt", "pepper"], | ||
| }; | ||
|
|
||
| console.log(`${recipe.title} serves ${recipe.serves} | ||
| ingredients: | ||
| ${recipe}`); | ||
| ingredients:`); | ||
|
|
||
| for (const ingredient of recipe.ingredients) { | ||
| console.log(ingredient); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,11 @@ | ||
| function contains() {} | ||
| function contains(obj, propName) { | ||
| // Return false for non-objects or null | ||
| if (obj === null || typeof obj !== "object" || Array.isArray(obj)) { | ||
| return false; | ||
| } | ||
|
|
||
| // Check if the object has the property as its own key | ||
| return Object.hasOwn(obj, propName); | ||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,57 @@ | ||
| 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 | ||
| as the object contains a key of 'a' | ||
|
|
||
|
|
||
| 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 | ||
|
|
||
|
|
||
| // 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({}, "a")).toBe(false); | ||
| }); | ||
|
|
||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
| test("contains returns true for existing property", () => { | ||
| 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 returns false for non-existent property", () => { | ||
| 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("contains returns false when first argument is an array even if key exists", () => { | ||
| const arr = ["a", "b", "c"]; | ||
|
|
||
| expect(contains(arr, "0")).toBe(false); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,12 @@ | ||
| function createLookup() { | ||
| function createLookup(countryCurrencyPairs) { | ||
| // implementation here | ||
| const lookup = {}; | ||
|
|
||
| for (const [countryCode, currencyCode] of countryCurrencyPairs) { | ||
| lookup[countryCode] = currencyCode; | ||
| } | ||
|
|
||
| return lookup; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,14 +3,32 @@ function parseQueryString(queryString) { | |
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
|
|
||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| if (pair === "") { | ||
| continue; | ||
| } | ||
|
|
||
| const indexOfEquals = pair.indexOf("="); | ||
| let key; | ||
| let value; | ||
|
|
||
| if (indexOfEquals === -1) { | ||
| // no "=", treat whole pair as key with empty value | ||
| key = pair; | ||
| value = ""; | ||
| } else { | ||
| key = pair.slice(0, indexOfEquals); | ||
| value = pair.slice(indexOfEquals + 1); | ||
| } | ||
|
|
||
| queryParams[key] = value; | ||
|
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. Note: (No change required)
|
||
| } | ||
|
|
||
| return queryParams; | ||
| } | ||
|
|
||
|
|
||
| module.exports = parseQueryString; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,15 @@ | ||
| function tally() {} | ||
| function tally(items) { | ||
| if (!Array.isArray(items)) { | ||
| throw new Error("tally expects an array"); | ||
| } | ||
|
|
||
| module.exports = tally; | ||
| const counts = Object.create(null); | ||
|
|
||
| for (const item of items) { | ||
| counts[item] = (counts[item] || 0) + 1; | ||
| } | ||
|
|
||
| return counts; | ||
| } | ||
|
|
||
| module.exports = tally; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,49 @@ | ||
| // Let's define how invert should work | ||
|
|
||
|
|
||
| // Given an object | ||
| // When invert is passed this object | ||
| // Then it should swap the keys and values in the object | ||
|
|
||
|
|
||
| // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} | ||
|
|
||
|
|
||
| function invert(obj) { | ||
| const invertedObj = {}; | ||
|
|
||
| for (const [key, value] of Object.entries(obj)) { | ||
| invertedObj.key = value; | ||
| invertedObj[value] = key; | ||
| } | ||
|
|
||
| return invertedObj; | ||
| } | ||
|
|
||
| module.exports = invert; | ||
|
|
||
|
|
||
| // a) What is the current return value when invert is called with { a : 1 } | ||
| // Answer: { key: 1 } | ||
|
|
||
|
|
||
| // b) What is the current return value when invert is called with { a: 1, b: 2 } | ||
| // Answer: { key: 2 } | ||
|
|
||
cjyuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // c) What is the target return value when invert is called with {a : 1, b: 2} | ||
| // Answer: { "1": "a", "2": "b" } | ||
|
|
||
|
|
||
| // c) What does Object.entries return? Why is it needed in this program? | ||
| // Answer: Object.entries(obj) returns an array of [key, value] pairs, | ||
| // It is needed so we can loop over the object and easily get both key and value in the for...of loop using array destructuring: [key, value]. | ||
|
|
||
|
|
||
| // d) Explain why the current return value is different from the target output | ||
| // Answer: In this implementation, the current return value already matches the target output, because we correctly use the value as the new key | ||
| // and the key as the new value: invertedObj[value] = key. | ||
|
|
||
|
|
||
| // e) Fix the implementation of invert (and write tests to prove it's fixed!) | ||
| // Answer: No changes needed to the implementation; it already works as required. | ||
|
|
||
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.
Your code works.
Here is an alternative worth exploring: Since ingredient values are separated by '\n' in the output, we could also use
Array.prototype.join()to construct the equivalent string and then output the resulting string.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.
It seems more complex at first glance.
I will check out later how it works, as I don't fully understand reading it.
Thanks