Repository navigation
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 9 commits
21ac6a2
31437ab
132dcb4
bfed0ed
16db858
d7c4016
ec2b1d0
928a880
330582e
c5fcec5
33087cd
7280413
6f021ec
f576655
6624821
80ebffb
cf78d67
9d3cc7d
740c965
1249b72
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,3 +1,12 @@ | ||
| function contains() {} | ||
|
|
||
| function contains(object, property) { | ||
| if (Array.isArray(object)) { | ||
| throw new Error("Expected an object, received an array"); | ||
| } | ||
|
cjyuan marked this conversation as resolved.
Resolved
|
||
| const obj = Object.keys(object); | ||
| if (obj.length === 0) { | ||
| return false; | ||
| } | ||
| return obj.includes(property); | ||
|
cjyuan marked this conversation as resolved.
Resolved
|
||
| } | ||
| 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); | ||
|
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. Is it necessary to add a suffix
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. It is'nt, the variables are in different scopes, i have removed the suffixes |
||
| } | ||
| ); | ||
|
|
||
| // 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"; | ||
|
cjyuan marked this conversation as resolved.
Resolved
|
||
| 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 |
|---|---|---|
| @@ -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; | ||
| }, {}); | ||
|
cjyuan marked this conversation as resolved.
Resolved
|
||
| } | ||
| 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.
The
valuein the loop is actually the key or property of the object. Naming itvalueis a bit misleading.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.
changed the variable name to key, thank you