Skip to content

Commit e720a7c

Browse files
authored
Enhance contains function with input validation
Add validation to the contains function to check if the input is a valid object and if the property exists.
1 parent 5e4611f commit e720a7c

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

‎Sprint-2/implement/contains.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
function contains() {}
1+
function contains() {
2+
3+
// Return false if obj is null, undefined, an array, or not a non-null object
4+
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
5+
return false;
6+
}
7+
8+
// Check if the property exists directly on the object
9+
return Object.prototype.hasOwnProperty.call(obj, prop);
10+
11+
}
212

313
module.exports = contains;

0 commit comments

Comments
 (0)