Skip to content

Commit 230981b

Browse files
committed
i have fixed contains and tally file
1 parent fd5c9ca commit 230981b

3 files changed

Lines changed: 19 additions & 5 deletions

File tree

‎Sprint-2/implement/contains.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
function contains(keyInput, valueInput) {
2-
if (Array.isArray(keyInput) == true) {
2+
if (
3+
keyInput == null ||
4+
typeof keyInput !== "object" ||
5+
Array.isArray(keyInput)
6+
) {
37
return false;
48
} else {
59
for (const key in keyInput) {
@@ -10,5 +14,5 @@ function contains(keyInput, valueInput) {
1014
}
1115
return false;
1216
}
13-
17+
// make values like null, undefined ,1234, true,"Abc" to be consider invalid and return false same as array
1418
module.exports = contains;

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,14 @@ test("Give a contains with object and property and if the property is non-existe
1515
test("Give invalid parameter in this case array when passed to contains it returns false", () => {
1616
expect(contains(["a", 2, "b", 3, 2], 2)).toEqual(false);
1717
});
18+
19+
test("Give invalid parameter in this case empty when passed to contains it returns false", () => {
20+
expect(contains(null)).toEqual(false);
21+
});
22+
test("returns false for undefined", () => {
23+
expect(contains(undefined, "key")).toEqual(false);
24+
});
25+
26+
test("returns false for numbers", () => {
27+
expect(contains(1234, "key")).toEqual(false);
28+
});

‎Sprint-2/implement/tally.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ function tally(items) {
44
}
55
const result = {};
66
for (const item of items) {
7-
result[item] = (result[item] || 0) + 1;
7+
const count = Object.hasOwn(result, item) ? result[item] : 0;
8+
result[item] = count + 1;
89
}
9-
1010
return result;
1111
}
12-
1312
module.exports = tally;

0 commit comments

Comments
 (0)