-
-
Notifications
You must be signed in to change notification settings - Fork 219
London | 25-ITP-SEP | Shaghayegh Shirinfar | Sprint 2 | Sprint2 exersices #816
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
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 |
|---|---|---|
|
|
@@ -4,12 +4,24 @@ | |
| // Each ingredient should be logged on a new line | ||
| // How can you fix it? | ||
|
|
||
| //answer : | ||
| // ${recipe.title} → "bruschetta" | ||
|
|
||
| // ${recipe.serves} → 2 | ||
|
|
||
| // ${recipe} → wrong! = > because recipe is an object, and when you interpolate it into a string, it becomes: | ||
|
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. Just as an extension: in javascript every object will have a toString() method which is what is called here when we interpolate it as a string. This method can actually be modified directly so that you customise what happens when console.log is called on it. Could you show me how you would add this method to this object?
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. recipe.toString = function () { console.log(String(recipe)); 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. Nice one, just missing the quotes as above |
||
|
|
||
| // [object Object] | ||
|
|
||
| const recipe = { | ||
| title: "bruschetta", | ||
| serves: 2, | ||
| ingredients: ["olive oil", "tomatoes", "salt", "pepper"], | ||
| }; | ||
|
|
||
| console.log(`${recipe.title} serves ${recipe.serves} | ||
| ingredients: | ||
| ${recipe}`); | ||
| console.log(recipe.title + "serves" + recipe.serves); | ||
| console.log("ingredients:"); | ||
|
|
||
| for (let i = 0; i < recipe.ingredients.length; i++) { | ||
| console.log(recipe.ingredients[i]); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,13 @@ | ||
| function contains() {} | ||
| // Implement the contains function | ||
| function contains(obj, propName) { | ||
| // Check that the first argument is an object, not null or an array | ||
| if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { | ||
| return false; | ||
| } | ||
|
|
||
| // Check if the object has the property | ||
| return obj.hasOwnProperty(propName); | ||
| } | ||
|
|
||
| // Export the function so other files can use it | ||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,22 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| // This function takes an array of country-currency pairs | ||
| // and turns it into an object where keys are country codes | ||
| // and values are currency codes | ||
| function createLookup(pairs) { | ||
| // Create an empty object to store the result | ||
| const lookup = {}; | ||
|
|
||
| // Loop through each pair in the array | ||
| for (let i = 0; i < pairs.length; i++) { | ||
| const pair = pairs[i]; // e.g. ['US', 'USD'] | ||
| const country = pair[0]; // first element is country code | ||
|
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. As an extension: could you show me how you can use array destructuring to define country and currency in one line?
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. const [country , currency] = pair[i]; |
||
| const currency = pair[1]; // second element is currency code | ||
|
|
||
| lookup[country] = currency; // add it to the object | ||
| } | ||
|
|
||
| // Return the final lookup object | ||
| return lookup; | ||
| } | ||
|
|
||
| // Export the function so it can be used in tests or other files | ||
| module.exports = createLookup; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,45 @@ | ||
| const createLookup = require("./lookup.js"); | ||
|
|
||
| test.todo("creates a country currency code lookup for multiple codes"); | ||
|
|
||
| /* | ||
|
|
||
| Create a lookup object of key value pairs from an array of code pairs | ||
|
|
||
| Acceptance Criteria: | ||
|
|
||
| Given | ||
| - An array of arrays representing country code and currency code pairs | ||
| e.g. [['US', 'USD'], ['CA', 'CAD']] | ||
|
|
||
| When | ||
| - createLookup function is called with the country-currency array as an argument | ||
|
|
||
| Then | ||
| - It should return an object where: | ||
| - The keys are the country codes | ||
| - The values are the corresponding currency codes | ||
|
|
||
| Example | ||
| Given: [['US', 'USD'], ['CA', 'CAD']] | ||
|
|
||
| When | ||
| createLookup(countryCurrencyPairs) is called | ||
|
|
||
| Then | ||
| It should return: | ||
| { | ||
| 'US': 'USD', | ||
| 'CA': 'CAD' | ||
| } | ||
| Implement a function called createLookup that turns an array of | ||
| country-currency code pairs into an object. | ||
| */ | ||
|
|
||
| // Test 1: multiple pairs | ||
| test("creates a country currency code lookup for multiple codes", () => { | ||
| const input = [ | ||
| ["US", "USD"], | ||
| ["CA", "CAD"], | ||
| ["GB", "GBP"], | ||
| ]; | ||
| const expected = { | ||
| US: "USD", | ||
| CA: "CAD", | ||
| GB: "GBP", | ||
| }; | ||
| expect(createLookup(input)).toEqual(expected); | ||
| }); | ||
|
|
||
| // Test 2: empty array should return empty object | ||
| test("returns empty object when given an empty array", () => { | ||
| const input = []; | ||
| const expected = {}; | ||
| expect(createLookup(input)).toEqual(expected); | ||
| }); | ||
|
|
||
| // Test 3: single pair | ||
| test("works with a single country-currency pair", () => { | ||
| const input = [["JP", "JPY"]]; | ||
| const expected = { JP: "JPY" }; | ||
| expect(createLookup(input)).toEqual(expected); | ||
| }); | ||
|
|
||
| // Test 4: duplicate keys (last one wins) | ||
| test("handles duplicate country codes (last value wins)", () => { | ||
| const input = [ | ||
| ["US", "USD"], | ||
| ["US", "USN"], | ||
| ]; | ||
| const expected = { US: "USN" }; // last one overwrites | ||
| expect(createLookup(input)).toEqual(expected); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,31 @@ | ||
| // In the prep, we implemented a function to parse query strings. | ||
| // Unfortunately, it contains several bugs! | ||
| // Below is one test case for an edge case the implementation doesn't handle well. | ||
| // Fix the implementation for this test, and try to think of as many other edge cases as possible - write tests and fix those too. | ||
| const parseQueryString = require("./querystring.js"); | ||
|
|
||
| const parseQueryString = require("./querystring.js") | ||
| // Test 1: normal key=value pairs | ||
| test("parses simple query string", () => { | ||
| expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" }); | ||
| }); | ||
|
|
||
| // Test 2: value contains '=' | ||
| test("parses querystring values containing =", () => { | ||
| expect(parseQueryString("equation=x=y+1")).toEqual({ | ||
| "equation": "x=y+1", | ||
| }); | ||
| expect(parseQueryString("equation=x=y+1")).toEqual({ equation: "x=y+1" }); | ||
| }); | ||
|
|
||
| // Test 3: empty string returns empty object | ||
| test("returns empty object for empty query string", () => { | ||
| expect(parseQueryString("")).toEqual({}); | ||
| }); | ||
|
|
||
| // Test 4: key without value | ||
| test("parses key without value as empty string", () => { | ||
| expect(parseQueryString("keyWithoutValue")).toEqual({ keyWithoutValue: "" }); | ||
| }); | ||
|
|
||
| // Test 5: multiple keys including empty value | ||
| test("parses multiple keys including empty values", () => { | ||
| expect(parseQueryString("a=1&b=&c=3")).toEqual({ a: "1", b: "", c: "3" }); | ||
| }); | ||
|
|
||
| // Test 6: only & characters with empty keys | ||
| test("handles consecutive & correctly", () => { | ||
| expect(parseQueryString("a=1&&b=2")).toEqual({ a: "1", b: "2", "": "" }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,23 @@ | ||
| function tally() {} | ||
| function tally(items) { | ||
| // Check if input is an array | ||
| if (!Array.isArray(items)) { | ||
| throw new Error("Input must be an array"); | ||
| } | ||
|
|
||
| const result = {}; | ||
|
|
||
| // Loop through each item in the array | ||
| for (const item of items) { | ||
| // If the item already exists in result, increment the count | ||
| if (result[item]) { | ||
| result[item] += 1; | ||
| } else { | ||
| // Otherwise, initialize the count to 1 | ||
| result[item] = 1; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| module.exports = tally; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,26 @@ | ||
| const tally = require("./tally.js"); | ||
|
|
||
| /** | ||
| * tally array | ||
| * | ||
| * In this task, you'll need to implement a function called tally | ||
| * that will take a list of items and count the frequency of each item | ||
| * in an array | ||
| * | ||
| * For example: | ||
| * | ||
| * tally(['a']), target output: { a: 1 } | ||
| * tally(['a', 'a', 'a']), target output: { a: 3 } | ||
| * tally(['a', 'a', 'b', 'c']), target output: { a : 2, b: 1, c: 1 } | ||
| */ | ||
| // Test 1: empty array should return empty object | ||
| test("tally on an empty array returns an empty object", () => { | ||
| expect(tally([])).toEqual({}); | ||
| }); | ||
|
|
||
| // Acceptance criteria: | ||
| // Test 2: array with single item | ||
| test("counts a single item", () => { | ||
| expect(tally(["a"])).toEqual({ a: 1 }); | ||
| }); | ||
|
|
||
| // Given a function called tally | ||
| // When passed an array of items | ||
| // Then it should return an object containing the count for each unique item | ||
| // Test 3: array with multiple duplicates | ||
| test("counts duplicates correctly", () => { | ||
| expect(tally(["a", "a", "a"])).toEqual({ a: 3 }); | ||
| }); | ||
|
|
||
| // Given an empty array | ||
| // When passed to tally | ||
| // Then it should return an empty object | ||
| test.todo("tally on an empty array returns an empty object"); | ||
| // Test 4: array with multiple unique items | ||
| test("counts multiple unique items correctly", () => { | ||
| expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 }); | ||
| }); | ||
|
|
||
| // Given an array with duplicate items | ||
| // When passed to tally | ||
| // Then it should return counts for each unique item | ||
|
|
||
| // Given an invalid input like a string | ||
| // When passed to tally | ||
| // Then it should throw an error | ||
| // Test 5: invalid input should throw an error | ||
| test("throws an error if input is not an array", () => { | ||
| expect(() => tally("not an array")).toThrow("Input must be an array"); | ||
| }); |
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.
Just as an extension, what could you do instead if you wanted to console.log key:value together?
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.
for (const [key, value] of Object.entries(author)) {
console.log(
${key}: ${value});}
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.
Very good! Don't forget the brackets around the $ sign, console.log(
${key}: ${value});