LONDON | MAY 2025 | JESUS DEL MORAL | SPRINT 2 | DATA GROUPS#647
LONDON | MAY 2025 | JESUS DEL MORAL | SPRINT 2 | DATA GROUPS#647delmorallopez wants to merge 6 commits intoCodeYourFuture:mainfrom
Conversation
| return false; | ||
| } | ||
|
|
||
| return Object.values(input).includes(containsvalues); |
There was a problem hiding this comment.
You could also 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
For more info, you can look up JS "in" operator vs Object.hasOwn.
There was a problem hiding this comment.
To check -- Property name (key) exists, we use propertyName in obj or hasOwn(obj, key)
To check -- Property value exists, we use Object.values(obj).includes(value)
const valueExists = Object.values(input).includes(value);
const keyExists = Object.hasOwn(input, value);
return valueExists || keyExists;
| const input = { | ||
| firstName: "Zaida", | ||
| lastName: "Smith", | ||
| occupation: "writer", | ||
| age: 40, | ||
| alive: true, | ||
| }; | ||
|
|
||
| const currentOutput = contains(input, "errortest"); | ||
| const targetOutput = false; | ||
|
|
||
| expect(currentOutput).toEqual(targetOutput); | ||
|
|
There was a problem hiding this comment.
-
Why are these parameters considered invalid?
-
Can you make your function return the following expected result?
// An array is a kind of object, and its keys are its indexes.
contains(["A", "B", "C"], "1"); // expect false because the first parameter is an array
// Empty string can also be key
contains({ "": "Boo" }, ""); // expect true
contains(null, "null"); // expect false even though type of null is equal to "object"
There was a problem hiding this comment.
The parameters are considers invalid because "errortest" is not a Key or value fo the object
I added all the tests considering the expected result on your suggestion
function contains(input, value) {
if (
input === null || // explicitly reject null
typeof input !== 'object' ||
Array.isArray(input) ||
value === undefined || value === null // allow empty string
) {
return false;
}
const valueExists = Object.values(input).includes(value);
const keyExists = Object.hasOwn(input, value);
return valueExists || keyExists;
}
|
Changes look good! Well done.
|
Sprint 2 Module Data Groups