-
-
Notifications
You must be signed in to change notification settings - Fork 273
Manchester | 26-ITP-Jan | Mehroz Munir | Sprint 2 | Coursework #1051
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
8ae3d62
e84acb9
baf14c8
f095515
02e014a
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,6 @@ | ||
| function contains() {} | ||
|
|
||
| function contains(object, property) { | ||
| if (object === null || typeof object !== "object" || Array.isArray(object)) | ||
| throw new Error("Input is not a valid object"); | ||
| else return Object.hasOwn(object, property); | ||
| } | ||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,29 +7,85 @@ 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 | ||
| describe("containsProperty", () => { | ||
| // 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 | ||
| [ | ||
| { input: { a: 1, b: 2 }, property: "a", expected: true }, | ||
| { input: { a: 1, b: 2 }, property: "c", expected: false }, | ||
| { input: { a: 1, b: 2, sdk: 28299 }, property: "what", expected: false }, | ||
| ].forEach(({ input, property, expected }) => | ||
| it(`Should return true if the object ${input} contains the property ${property} otherwise false`, () => | ||
| expect(contains(input, property)).toEqual(expected)) | ||
| ); | ||
|
|
||
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| test.todo("contains on empty object returns false"); | ||
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| [ | ||
| { input: {}, property: "a", expected: false }, | ||
| { input: {}, property: "", expected: false }, | ||
| { input: {}, property: null, expected: false }, | ||
| ].forEach(({ input, property, expected }) => | ||
| it(`Should return false if the object ${input} is empty`, () => | ||
| expect(contains(input, property)).toEqual(expected)) | ||
| ); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
| [ | ||
| { input: { a: 1, dog: 2 }, property: "dog", expected: true }, | ||
| { | ||
| input: { a: 1, b: 2, property3: "here you go" }, | ||
| property: "property3", | ||
| expected: true, | ||
| }, | ||
| { input: { a: 1, b: 2, sdk: 28299 }, property: "sdk", expected: true }, | ||
| ].forEach(({ input, property, expected }) => | ||
| it(`Should return true if the object ${input} contains the property ${property}`, () => | ||
| expect(contains(input, property)).toEqual(expected)) | ||
| ); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| [ | ||
| { input: { a: 1, dog: 2 }, property: "cat", expected: false }, | ||
| { | ||
| input: { a: 1, b: 2, property3: "here you go" }, | ||
| property: "", | ||
| expected: false, | ||
| }, | ||
| { | ||
| input: { a: 1, b: 2, thatProperty: 28299 }, | ||
| property: "", | ||
| expected: false, | ||
| }, | ||
| ].forEach(({ input, property, expected }) => | ||
| it(`Should return false if the object ${input} doesn't contain the property ${property}`, () => | ||
| expect(contains(input, property)).toEqual(expected)) | ||
| ); | ||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| [ | ||
| { input: [1], property: "a" }, | ||
cjyuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { input: [1], property: "0" }, | ||
|
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. My concern was just about the test. We write tests not only to verify our current implementation, but also to ensure that future changes do not alter the function's expected behavior. So after adding a test such as
you can help ensure future modification of the function can still correctly reject array. |
||
| { input: null, property: "c" }, | ||
| { input: undefined, property: "what" }, | ||
| { input: [1, 2, 3], property: "what" }, | ||
| ].forEach(({ input, property }) => | ||
| it(`Should return false as ${input} is not an object`, () => | ||
| expect(() => contains(input, property)).toThrow( | ||
| "Input is not a valid object" | ||
| )) | ||
| ); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| function createLookup() { | ||
| function createLookup(array) { | ||
| // implementation here | ||
| if (!Array.isArray(array) || !array.every((item) => Array.isArray(item))) | ||
| throw new Error("Invalid Input"); | ||
| return Object.fromEntries(array); | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,10 @@ | ||
| function tally() {} | ||
|
|
||
| function tally(array) { | ||
| if (!Array.isArray(array)) throw new Error("Not an array"); | ||
| const tallyObject = {}; | ||
| for (const item of array) { | ||
| if (Object.hasOwn(tallyObject, item)) tallyObject[item] += 1; | ||
| else tallyObject[item] = 1; | ||
| } | ||
| return tallyObject; | ||
| } | ||
| module.exports = tally; |
Uh oh!
There was an error while loading. Please reload this page.
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.
Note: This function can be replaced by a single function call to
Array.prototype.join().No change required but that array's method is worth exploring.