-
-
Notifications
You must be signed in to change notification settings - Fork 323
London | 26-ITP-May | Damilola Odumosu | Sprint 2 | Coursework #1352
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
21ac6a2
31437ab
132dcb4
bfed0ed
16db858
d7c4016
ec2b1d0
928a880
330582e
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 |
|---|---|---|
|
|
@@ -11,5 +11,7 @@ const recipe = { | |
| }; | ||
|
|
||
| console.log(`${recipe.title} serves ${recipe.serves} | ||
| ingredients: | ||
| ${recipe}`); | ||
| ingredients:`); | ||
| for (const value of Object.values(recipe.ingredients)) { | ||
|
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. Why not use the for-of loop to iterate through the array |
||
| console.log(value); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,12 @@ | ||
| function contains() {} | ||
|
|
||
| function contains(object, property) { | ||
| if (Array.isArray(object)) { | ||
| throw new Error("Expected an object, received an array"); | ||
| } | ||
|
Comment on lines
+2
to
+4
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. What if |
||
| const obj = Object.keys(object); | ||
| if (obj.length === 0) { | ||
| return false; | ||
| } | ||
| return obj.includes(property); | ||
|
Comment on lines
+5
to
+9
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. Could also explore |
||
| } | ||
| console.log(contains({ 1: "o", 2: "k" }, 1)); | ||
| module.exports = contains; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ 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' | ||
| as the object doesn't contain a key of 'c' | ||
| */ | ||
|
|
||
| // Acceptance criteria: | ||
|
|
@@ -17,19 +17,65 @@ 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 | ||
|
|
||
| test( | ||
| "when passed an object and a property name, should return true if the object " + | ||
| "contains the property", | ||
| () => { | ||
| const obj1 = { | ||
| name: "dami", | ||
| age: 34, | ||
| height: 5.3, | ||
| location: "London", | ||
| }; | ||
| const propertyNameCheck1 = "name"; | ||
| const expected1 = true; | ||
| const result1 = contains(obj1, propertyNameCheck1); | ||
| expect(result1).toBe(expected1); | ||
| } | ||
| ); | ||
|
|
||
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| test.todo("contains on empty object returns false"); | ||
|
|
||
| test("given an empty object, should return false", () => { | ||
| const obj2 = {}; | ||
| const propertyNameCheck2 = "a"; | ||
| const expected2 = false; | ||
| const result2 = contains(obj2, propertyNameCheck2); | ||
| expect(result2).toBe(expected2); | ||
| }); | ||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
| //this test requirement is covered on line 21 | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| test( | ||
| "when passed an object and a property name, should return false if the object " + | ||
| "does not contain the property.", | ||
| () => { | ||
| const obj3 = { | ||
| title: "Things fall apart", | ||
| year: 1958, | ||
| publisher: "pan-macmillan", | ||
| }; | ||
| const propertyNameCheck3 = "location"; | ||
| const expected3 = false; | ||
| const result3 = contains(obj3, propertyNameCheck3); | ||
| expect(result3).toBe(expected3); | ||
| } | ||
| ); | ||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("when passed a data type other than an object, throw an error", () => { | ||
| const arr = []; | ||
| const propertyNameCheck4 = "name"; | ||
|
Comment on lines
+76
to
+77
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. Arrays are objects, with their indices acting as keys. A proper test should use a non-empty array along with a valid key to ensure the function returns |
||
| expect(() => { | ||
| contains(arr, propertyNameCheck4); | ||
| }).toThrow("Expected an object, received an array"); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,28 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| } | ||
| function createLookup(arr) { | ||
| if (!Array.isArray(arr)) { | ||
| throw new Error("invalid data type entered"); | ||
| } | ||
| if (arr.length === 0) { | ||
| throw new Error("country and currency code not entered"); | ||
| } | ||
|
|
||
| const objectLookup = {}; | ||
|
|
||
| for (const pair of arr) { | ||
| if (!Array.isArray(pair)) { | ||
| throw new Error("Each item must be an array."); | ||
| } | ||
| if (pair.length !== 2) { | ||
| throw new Error( | ||
| "Each inner array must contain exactly two elements: a key and a value" | ||
| ); | ||
| } | ||
| const [country, currency] = pair; | ||
| if (country === currency) { | ||
| throw new Error("Country code and currency code cannot be the same"); | ||
| } | ||
| objectLookup[country] = currency; | ||
| } | ||
| return objectLookup; | ||
| } | ||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,10 +6,19 @@ function parseQueryString(queryString) { | |
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| const indexOfFirst = pair.indexOf("="); | ||
| if (indexOfFirst === -1) { | ||
| queryParams[pair] = ""; | ||
|
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: The value of |
||
| } else { | ||
| let key = decodeURIComponent( | ||
| pair.slice(0, indexOfFirst).replaceAll("+", " ") | ||
| ); | ||
| let value = decodeURIComponent( | ||
| pair.slice(indexOfFirst + 1).replaceAll("+", " ") | ||
| ); | ||
| queryParams[key] = value; | ||
| } | ||
| } | ||
|
|
||
| return queryParams; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,13 @@ | ||
| function tally() {} | ||
|
|
||
| function tally(arr) { | ||
| if (!Array.isArray(arr)) { | ||
| throw new Error('Invalid data type entered"'); | ||
| } | ||
| if (arr.length === 0) { | ||
| return {}; | ||
| } | ||
| return arr.reduce((acc, cur) => { | ||
| acc[cur] = (acc[cur] || 0) + 1; | ||
| return acc; | ||
| }, {}); | ||
|
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:
|
||
| } | ||
| 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.
Why not just call the function as
console.log(author[value])?