-
-
Notifications
You must be signed in to change notification settings - Fork 283
London | 26-ITP-Jan | Damian Dunkley | Sprint 2 | Data-Groups #993
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
a0f6c7e
ed88192
bc4aae5
9d340fe
95b92ba
1af24f2
4edc7ed
0eba06a
cba3098
2ee1655
b004cf4
2a7eae5
07a67c7
24e9bac
728710e
45dd976
f886f54
aba13bc
7adcbd8
36df520
3698abb
fbd948c
9879390
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,10 @@ | ||
| function contains() {} | ||
| function contains(obj, prop) { | ||
| return ( | ||
| !!obj && // confirm that obj is not null or undefined. If undefined or null, return false. | ||
| typeof obj === "object" && // confirm that obj is an object. If not an object, return false. | ||
| !Array.isArray(obj) && // confirm that obj is not an array. If an array, return false. | ||
| Object.hasOwnProperty.call(obj, prop)// check if the object has the property. If it does, return true; otherwise, return false. | ||
| ); | ||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,20 +16,48 @@ as the object doesn't contains a key of 'c' | |
| // 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 | ||
| test("contains returns true for existing property", () => { | ||
| const obj = { a: 1, b: 2 }; | ||
| expect(contains(obj, "a")).toBe(true); | ||
| expect(contains(obj, "b")).toBe(true); | ||
| expect(contains(obj, "c")).toBe(false); | ||
| }); | ||
|
|
||
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| test.todo("contains on empty object returns false"); | ||
| test("contains on empty object returns false", () => { | ||
| const emptyObj = {}; | ||
| expect(contains(emptyObj, "a")).toBe(false); | ||
| expect(contains(emptyObj, "b")).toBe(false); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
| test("contains returns true for existing property", () => { | ||
| const obj2 = { x: 10, y: 20 }; | ||
| expect(contains(obj2, "x")).toBe(true); | ||
| expect(contains(obj2, "y")).toBe(true); | ||
| expect(contains(obj2, "z")).toBe(false); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| test("contains returns false for non-existent property", () => { | ||
| const obj3 = { name: "Alice", age: 30 }; | ||
| expect(contains(obj3, "name")).toBe(true); | ||
| expect(contains(obj3, "age")).toBe(true); | ||
| expect(contains(obj3, "gender")).toBe(false); | ||
| }); | ||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("contains returns false for invalid input types", () => { | ||
| const arr = [1, 2, 3]; | ||
| expect(contains(arr, "0")).toBe(false); // Arrays do not have properties like objects, so it should return false | ||
|
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: An array is a kind of objects and its indexes are its keys (properties). So for an implementation that does not check if the given value is an array using
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 think that you're saying that my use of !Array.isArray(obj) as a check only works because I have included the property and if I had !Array.isArray(), this would allow 0 to be found in an array of 1, 2, 3 because 1 has a property of "0". Have I understood that correctly or are you expecting me to make changes? Thanks
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. Sorry that my comment wasn't clear enough. I meant say the description given in the comment is not quite correct.
The function should return false because the first argument is an array (and not because it does not have properties). |
||
| expect(contains(null, "a")).toBe(false); // null is not an object, so it should return false | ||
| expect(contains(undefined, "a")).toBe(false); // undefined is not an object, so it should return false | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,23 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(pairs) { | ||
| if (!Array.isArray(pairs)) { | ||
| // check if the input is an array. If not, throw an error. | ||
| throw new Error("Input must be an array of pairs"); | ||
| } | ||
|
|
||
| const lookup = {}; // create an empty object to store key-value pairs | ||
|
|
||
| for (const pair of pairs) { | ||
| // iterate through each pair in the input array | ||
| if (!Array.isArray(pair) || pair.length !== 2) { | ||
| // check if the current pair is an array of length 2. If not, throw an error. | ||
| throw new Error("Each pair must be an array of two elements"); | ||
| } | ||
|
|
||
| const key = pair[0]; // extract the first element of the pair as the key | ||
| const value = pair[1]; // extract the second element of the pair as the value | ||
| lookup[key] = value; // add the key-value pair to the lookup object | ||
| } | ||
| return lookup; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,12 @@ | ||
| function tally() {} | ||
| function tally(items) { | ||
| if (!Array.isArray(items)) { | ||
| throw new TypeError("Input must be an array"); | ||
| } | ||
|
|
||
| return items.reduce((counts, item) => { | ||
| counts[item] = (counts[item] ?? 0) + 1; | ||
| return counts; | ||
| }, Object.create(null)); | ||
| } | ||
|
|
||
| module.exports = tally; |
Uh oh!
There was an error while loading. Please reload this page.