-
-
Notifications
You must be signed in to change notification settings - Fork 327
London | 26-ITP-May | Edina Kurdi | Sprint 2 | Coursework #1422
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
6a0643a
6de1246
2488a71
0291dfa
bced4fe
5866558
3423b50
2a62f58
0b6fc65
9c07c98
95e7d98
4de66ef
078648c
6d32545
1e8685b
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,4 +1,5 @@ | ||
| // Predict and explain first... | ||
| //in the template literal the ${recipe}`is referring to the whole object, we want the items in the ingredients of the recipe object, listed line by line. | ||
|
|
||
| // This program should log out the title, how many it serves and the ingredients. | ||
| // Each ingredient should be logged on a new line | ||
|
|
@@ -10,6 +11,20 @@ const recipe = { | |
| ingredients: ["olive oil", "tomatoes", "salt", "pepper"], | ||
| }; | ||
|
|
||
| console.log(`${recipe.title} serves ${recipe.serves} | ||
| ingredients: | ||
| ${recipe}`); | ||
| // const recipeKeys = Object.keys(recipe) | ||
| // // console.log(recipeKeys) | ||
|
|
||
| const recipeValues = Object.values(recipe); | ||
| // ["bruschetta", 2, ["olive oil", "tomatoes", "salt", "pepper"]] | ||
|
|
||
| const ingredients = recipeValues.slice(-1)[0]; | ||
| // ["olive oil","tomatoes","salt","pepper"] | ||
|
|
||
|
Comment on lines
+17
to
+22
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. good use of methods but how could we reduce the amount of code we're writing here? Is there a more direct way to access the ingredients so you can join them on line 25?
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. oh, i seem to have created more work for myself than needed. corrected now |
||
| //create a function to log line by line the elements of an array | ||
| function logItemised(items) { | ||
| return items.join("\n"); | ||
| } | ||
|
|
||
| console.log( | ||
| `${recipe.title} serves ${recipe.serves}${"\n"}ingredients:${"\n"}${logItemised(ingredients)}` | ||
| ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,24 @@ | ||
| function contains() {} | ||
| function contains(object, propertyName) { | ||
| if (typeof object !== "object" || object === null || Array.isArray(object)) { | ||
| throw new Error("Input should be an object"); | ||
| } | ||
| const keysInObject = Object.keys(object); | ||
| if (keysInObject.includes(propertyName)) { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
Comment on lines
+6
to
+11
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. How could you refactor these lines to be more concise?
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. yes, since it returns a boolean, I dont have to write if else statement. done. |
||
|
|
||
| module.exports = contains; | ||
|
|
||
| /* | ||
| 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' | ||
| */ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,10 +17,23 @@ as the object doesn't contains a key of 'c' | |
| // When passed an object and a property name | ||
| // Then it should return true if the object contains the property, false otherwise | ||
|
|
||
| describe("when checking property existence", () => { | ||
| test("should return true if the object contains the property", () => { | ||
| expect(contains({ a: "apple", b: "hill" }, "a")).toBe(true); | ||
| }); | ||
| test("should return false if the object does not contain the property", () => { | ||
| expect(contains({ a: "apple", b: "hill" }, "c")).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| test.todo("contains on empty object returns false"); | ||
| describe("given an empty object", () => { | ||
| test("should return false when passed to contains", () => { | ||
| expect(contains({}, "a").toBe(false)); | ||
|
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 line fails - please correct it
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. oh those brackets... fixed now. |
||
| }); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
|
|
@@ -33,3 +46,18 @@ test.todo("contains on empty object returns false"); | |
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| describe("when given invalid inputs", () => { | ||
| test("should throw an error if the input is not an object", () => { | ||
| expect(() => contains([true, 2, "hill"], "2")).toThrow( | ||
| "Input should be an object" | ||
| ); | ||
| }); | ||
|
|
||
| test("should throw an error if the input is not an object", () => { | ||
| expect(() => contains(null, "hi")).toThrow("Input should be an object"); | ||
| }); | ||
|
|
||
| test("should throw an error if the input is not an object", () => { | ||
| expect(() => contains("apple", "a")).toThrow("Input should be an object"); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,52 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(countryCurrencyPairs) { | ||
| //if countryCurrencyPairs is not an array, throw error | ||
| if (!Array.isArray(countryCurrencyPairs)) { | ||
| throw new Error("Invalid input. It should be an array"); | ||
| } | ||
| //if it is an empty array, throw error | ||
| if (countryCurrencyPairs.length === 0) { | ||
| throw new Error("Input should not be an empty array"); | ||
| } | ||
|
|
||
| //if not all elements are an array in the array, throw an error | ||
| if (!countryCurrencyPairs.every(Array.isArray)) { | ||
| throw new Error("Invalid input. All elements should be arrays"); | ||
| } | ||
|
|
||
| return Object.fromEntries(countryCurrencyPairs); | ||
|
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 choice of method
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. thank you i came across it when learning about iterating through objects. very handy. |
||
| } | ||
|
|
||
| module.exports = createLookup; | ||
|
|
||
| // console.log( | ||
| // createLookup([ | ||
| // ["US", "USD"], | ||
| // ["CA", "CAD"], | ||
| // ]) | ||
| // ); | ||
|
|
||
| // console.log(createLookup([])); | ||
|
|
||
| /* | ||
| 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' | ||
| } | ||
|
|
||
| */ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,41 @@ | ||
| const createLookup = require("./lookup.js"); | ||
|
|
||
| test.todo("creates a country currency code lookup for multiple codes"); | ||
| describe("when given invalid inputs", () => { | ||
| test("should return invalid input error, if the input is an empty array", () => { | ||
| expect(() => createLookup([])).toThrow( | ||
| "Input should not be an empty array" | ||
| ); | ||
| }); | ||
| test("should return invalid input error, if the input isn't an array of arrays", () => { | ||
| expect(() => createLookup(["hi", "hello"])).toThrow( | ||
| "Invalid input. All elements should be arrays" | ||
| ); | ||
| }); | ||
| test("should return invalid input error, if the input isn't an array of arrays", () => { | ||
| expect(() => createLookup("hi")).toThrow( | ||
| "Invalid input. It should be an array" | ||
| ); | ||
| }); | ||
| test("should return invalid input error, if the input isn't an array of arrays", () => { | ||
| expect(() => createLookup(2)).toThrow( | ||
| "Invalid input. It should be an array" | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("when given valid inputs", () => { | ||
| test("should return an object where (Input ==> Output): keys:values ==> country code: corresponding currency", () => { | ||
| expect( | ||
| createLookup([ | ||
| ["US", "USD"], | ||
| ["CA", "CAD"], | ||
| ]) | ||
| ).toEqual({ | ||
| US: "USD", | ||
| CA: "CAD", | ||
| }); | ||
| }); | ||
| }); | ||
|
Comment on lines
+26
to
+38
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 you have included a simpler test to start with for valid inputs?
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. i added a simpler input with only one array in the array (one country's data) |
||
|
|
||
| /* | ||
|
|
||
|
|
@@ -9,7 +44,7 @@ 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 | ||
| - An array of arrays representing when given invalid inputs code pairs | ||
| e.g. [['US', 'USD'], ['CA', 'CAD']] | ||
|
|
||
| When | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,12 +3,37 @@ function parseQueryString(queryString) { | |
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| //replace initial "?" if present | ||
| if (queryString.startsWith("?")) { | ||
| queryString = queryString.replace("?", ""); | ||
| } | ||
| //replace encoded characters | ||
| queryString = decodeURIComponent(queryString); | ||
|
|
||
| //replace "+" with " " | ||
| queryString = queryString.replaceAll("+", " "); | ||
|
|
||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| //filter out empty strings from the keyValuePairs array of strings | ||
| let filteredKeyValuePairs = keyValuePairs.filter( | ||
|
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 this be a const?
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. yes, and i changed it now. (as above) I need to rememer to use const instead of let |
||
| (keyValuePair) => keyValuePair.length > 0 | ||
| ); | ||
|
|
||
| //assign key value pairs created by separating on the first "=" sign | ||
| filteredKeyValuePairs.forEach((str) => { | ||
| //if no "=", then the string should be the key | ||
| if (!str.includes("=")) { | ||
| const key = str; | ||
| queryParams[key] = ""; | ||
| } else { | ||
| const indexOfFirstEqual = str.indexOf("="); | ||
| const key = str.slice(0, indexOfFirstEqual); | ||
| const value = str.slice(indexOfFirstEqual + 1); | ||
| queryParams[key] = value; | ||
| } | ||
| }); | ||
|
|
||
| return queryParams; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,18 @@ | ||
| function tally() {} | ||
| function tally(array) { | ||
| if (typeof array === "string") { | ||
|
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. What if the input is a number or a boolean? How can you change this condition so it will hold up against any wrong input type?
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. yes, i took the instructions too literally :) |
||
| throw new Error("Input should be an array"); | ||
| } | ||
|
|
||
| let tallySet = {}; | ||
|
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. Would this code still work if you made the variable a constant? Explain your answer :)
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. yes, and updated it. |
||
|
|
||
| for (let item of array) { | ||
| if (!tallySet[item]) { | ||
| tallySet[item] = 1; | ||
| } else { | ||
| tallySet[item] += 1; | ||
| } | ||
| } | ||
| return tallySet; | ||
| } | ||
|
|
||
| module.exports = tally; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,16 +19,32 @@ const tally = require("./tally.js"); | |
| // Given a function called tally | ||
| // When passed an array of items | ||
| // Then it should return an object containing the count for each unique item | ||
| describe("tally()", () => { | ||
| // Given an array with duplicate items | ||
| // When passed to tally | ||
| // Then it should return counts for each unique item | ||
| describe("when given an array with duplicate items", () => { | ||
| test("should return an object with counts for each unique item", () => { | ||
| expect(tally(["a"])).toEqual({ a: 1 }); | ||
| expect(tally(["a", "a", "a"])).toEqual({ a: 3 }); | ||
| expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 }); | ||
|
Comment on lines
+28
to
+30
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. I actually think these are three different behaviours and so deserve their own test blocks to anyone reading them test suite can see the different behaviours clearly.
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. corrected. |
||
| }); | ||
| }); | ||
| // Given an empty array | ||
| // When passed to tally | ||
| // Then it should return an empty object | ||
| describe("when given an empty array", () => { | ||
| test("should return an empty object", () => { | ||
| expect(tally([])).toEqual({}); | ||
| }); | ||
| }); | ||
|
|
||
| // 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"); | ||
|
|
||
| // 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 | ||
| // Given an invalid input like a string | ||
| // When passed to tally | ||
| // Then it should throw an error | ||
| describe("when given invalid input such as a string", () => { | ||
| test("should throw an error", () => { | ||
| expect(() => tally("apple")).toThrow("Input should 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.
A good workaround to print the values of the object.
Question - how could the for loop on lines 21 - 23 be changed (to a different loop for example) to achieve the same effect?
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.
Created an alternative using a loop