Skip to content

Commit 943226e

Browse files
committed
Enhance contains function to handle arrays, undefined, and null inputs; add corresponding tests for these cases
1 parent 0118bc5 commit 943226e

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

Sprint-2/implement/contains.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
function contains(obj, prop) {
2+
if (Array.isArray(obj) || obj === undefined || obj === null) {
3+
return false;
4+
}
25
return obj.hasOwnProperty(prop);
36
}
47

Sprint-2/implement/contains.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,14 @@ describe("contains", () => {
5151
const arr = [1, 2, 3];
5252
expect(contains(arr, "a")).toEqual(false);
5353
});
54+
55+
test("contains returns false for arrays, even if the index exists", () => {
56+
const arr = ["a", "b", "c"];
57+
expect(contains(arr, "0")).toEqual(false);
58+
});
59+
60+
test("contains returns false for undefined", () => {
61+
expect(contains(undefined, "a")).toEqual(false);
62+
expect(contains(null, "a")).toEqual(false);
63+
});
5464
});

0 commit comments

Comments
 (0)