-
-
Notifications
You must be signed in to change notification settings - Fork 220
London | 25-ITP-SEP | Adnaan Abo | Sprint 2 | Coursework #807
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
Conversation
Sprint-2/implement/contains.js
Outdated
| * Checks if the given array contains the specified value. | ||
| * @param {Array} arr - The array to check. | ||
| * @param {*} value - The value to search for. | ||
| * @returns {boolean} - Returns true if the value is found, otherwise false. | ||
| */ |
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.
What does this comment describe?
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 comments from line 3 -6 describe a function that looks inside an array and tells you whether a certain value is present.
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.
What function would that be? Your function does not quite match what it describe:
- Its first parameter is not named
arr. - Its first parameter is not an array
- Its second parameter is not named
value.
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.
you are absolutely right it was one of those rookie mistake from my side. I have re-written the function to match the JSDoc
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.
-
Is the arguments passed to
containsan array? -
Object contains keys and values. Naming the 2nd parameter 'value' could be 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.
The description in contains.test.js says,
/*
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'
*/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.
No, we cannot assume the arguments passed to contains() are an array.
and i have also changed the parameter 'value' --> 'item'
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.
I have now redone the whole contains.js assignment.
Sprint-2/implement/contains.test.js
Outdated
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("contains on invalid parameters returns false", () => { | ||
| expect(contains([], "a")).toBe(false); |
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.
This test cannot detect (for certainty) if a function can properly check if the first parameter is an array or not.
An implementation of contains() could also return false because the property "a" cannot be found in the array.
Can you address this shortcoming?
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.
updated the the test we now only return false for invalid types.
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.
My point was, contains([], "a") will evaluate to false regardless of whether the function checks if its first parameter is an array or not. If you want to test if the function can reject array, you should write something like
contains(["A"], "0")
because "0" is a key of the array. If contains() does not reject array, then it would have returned true.
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.
After fixing the contains.js I re-wrote the test-cases
cjyuan
left a comment
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.
Almost done. One more question.
Sprint-2/implement/contains.js
Outdated
| typeof obj === "object" && | ||
| obj !== null && | ||
| !Array.isArray(obj) && | ||
| property in obj |
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.
Consider the following two approaches for determining if an object contains a property:
let obj = {}, propertyName = "toString";
console.log( propertyName in obj ); // true
console.log( Object.hasOwn(obj, propertyName) ); // false
Which of these approaches suits your needs better?
For more info, you can look up JS "in" operator vs Object.hasOwn.
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.
property in obj --> will return true for own properties and inherited properties. This will give false positives in my use-cases.
While:
Object.hasOwn(obj, property) --> will return true only for properties defined directly on the object
| test("Checks if an object contains the specified property", () => { | ||
| const obj1 = { name: "Alice", age: 30 }; | ||
| expect(contains(obj1, "name")).toBe(true); // The property "name" exists | ||
| expect(contains(obj1, "city")).toBe(false); // The property "city" does not exist | ||
| }); | ||
|
|
||
| // contains({ a: 1, b: 2 }, 'a') // returns true | ||
| test("Checks if an object contains the specified property", () => { | ||
| const obj1 = { a: 1, b: 2 }; | ||
| expect(contains(obj1, "a")).toBe(true); // The property "name" exists | ||
| }); | ||
|
|
||
| // contains({ a: 1, b: 2 }, 'c') // returns false | ||
| test("Checks if an object contains the specified property", () => { | ||
| const obj1 = { a: 1, b: 2 }; | ||
| expect(contains(obj1, "c")).toBe(false); // The property "c" does not exist | ||
| }); |
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.
Could consider grouping these tests into one category.
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.
these have been grouped
// contains({ a: 1, b: 2 }, 'a') // returns true and contains({ a: 1, b: 2 }, 'c') // returns false
test("Checks if an object contains the specified property", () => {
const obj1 = { a: 1, b: 2 };
expect(contains(obj1, "a")).toBe(true); // The property "name" exists
expect(contains(obj1, "c")).toBe(false); // The property "c" does not exist
});
cjyuan
left a comment
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.
Changes look good, and responses were clear.
| test("Checks if an object contains the specified property", () => { | ||
| const obj1 = { name: "Alice", age: 30 }; | ||
| expect(contains(obj1, "name")).toBe(true); // The property "name" exists | ||
| expect(contains(obj1, "city")).toBe(false); // The property "city" does not exist | ||
| }); | ||
|
|
||
| // contains({ a: 1, b: 2 }, 'a') // returns true and contains({ a: 1, b: 2 }, 'c') // returns false | ||
| test("Checks if an object contains the specified property", () => { | ||
| const obj1 = { a: 1, b: 2 }; | ||
| expect(contains(obj1, "a")).toBe(true); // The property "name" exists | ||
| expect(contains(obj1, "c")).toBe(false); // The property "c" does not exist | ||
| }); | ||
|
|
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.
These two tests are quite similar.
It would be more useful to test samples with different characteristics. For examples
- set obj1 to
{a : 'c', b: 2 }to ensure the function won't check the value of the properties. - Use property names with different letter case to ensure the check is case sensitive.
Self checklist
Changelist
Completed Sprint-2 exercises covering JavaScript challenges, error fixing code interpretation and running test-cases with Jest.