Skip to content

Commit 351dfc6

Browse files
author
Enice-Codes
committed
Complete contains project
1 parent de6889c commit 351dfc6

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

‎Sprint-2/implement/contains.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
function contains() {}
1+
function contains(obj, prop) {
2+
if (typeof obj !== "object" || obj === null) return false;
3+
return Object.prototype.hasOwnProperty.call(obj, prop);
4+
}
25

3-
module.exports = contains;
6+
module.exports = contains;

‎Sprint-2/implement/contains.test.js‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const contains = require("./contains.js");
1+
const contains = require("./contains")
22

33
/*
44
Implement a function called contains that checks an object contains a
@@ -16,20 +16,44 @@ as the object doesn't contains a key of 'c'
1616
// Given a contains function
1717
// When passed an object and a property name
1818
// Then it should return true if the object contains the property, false otherwise
19+
test("contains returns true for existing property", () => {
20+
const currentOutput = contains({a:1,b:2}, 'a');
21+
const targetOutput = true;
22+
expect(currentOutput).toEqual(targetOutput);
23+
});
1924

2025
// Given an empty object
2126
// When passed to contains
2227
// Then it should return false
23-
test.todo("contains on empty object returns false");
28+
test("contains on empty object returns false", () => {
29+
const currentOutput = contains({}, 'a');
30+
const targetOutput = false;
31+
expect(currentOutput).toEqual(targetOutput);
32+
});
2433

2534
// Given an object with properties
2635
// When passed to contains with an existing property name
2736
// Then it should return true
37+
test("contains returns true for existing property", () => {
38+
const currentOutput = contains({a:1,b:2}, 'b');
39+
const targetOutput = true;
40+
expect(currentOutput).toEqual(targetOutput);
41+
});
2842

2943
// Given an object with properties
3044
// When passed to contains with a non-existent property name
3145
// Then it should return false
46+
test("contains returns false for non-existent property", () => {
47+
const currentOutput = contains({a:1,b:2}, 'c');
48+
const targetOutput = false;
49+
expect(currentOutput).toEqual(targetOutput);
50+
});
3251

3352
// Given invalid parameters like an array
3453
// When passed to contains
3554
// Then it should return false or throw an error
55+
test("contains returns false for invalid parameters", () => {
56+
const currentOutput = contains([1,2,3], 'a');
57+
const targetOutput = false;
58+
expect(currentOutput).toEqual(targetOutput);
59+
});

0 commit comments

Comments
 (0)