Skip to content

Commit cfcdacd

Browse files
refactored the contains and tally functions to pass the test cases
1 parent 3cdb54e commit cfcdacd

3 files changed

Lines changed: 34 additions & 32 deletions

File tree

‎Sprint-2/implement/contains.js‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
function contains(object,result) {
2-
for(const key in object){
3-
if(key===result){
4-
return true
5-
}
1+
function contains(object, result) {
2+
const isPlainObject =
3+
typeof object === "object" && object !== null && !Array.isArray(object);
64

5+
if (!isPlainObject) {
6+
throw new Error("contains require an object");
7+
}
8+
for (const key in object) {
9+
if (key === result) {
10+
return true;
711
}
8-
return false
12+
}
13+
return false;
914
}
1015

11-
1216
module.exports = contains;

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,34 @@ as an array isn't an object
2323
// Given an empty object
2424
// When passed to contains
2525
// Then it should return false
26-
test("contains on empty object returns false",()=>{
27-
expect(contains({})).toEqual(false);
26+
test("contains on empty object returns false", () => {
27+
expect(contains({})).toEqual(false);
2828
});
2929

3030
// Given an object with properties
3131
// When passed to contains with an existing property name
3232
// Then it should return true
33-
test('contains passed an object and a property name, returns true',()=>{
34-
expect(contains({a:1,b:2},"a")).toEqual(true);
33+
test("contains passed an object and a property name, returns true", () => {
34+
expect(contains({ a: 1, b: 2 }, "a")).toEqual(true);
3535
});
3636
// Given an object with properties
3737
// When passed to contains with a non-existent property name
3838
// Then it should return false
39-
test('Contains passed with a non-existent property name, returns false',()=>{
40-
expect(contains({a:1,b:2},"c")).toEqual(false);
39+
test("Contains passed with a non-existent property name, returns false", () => {
40+
expect(contains({ a: 1, b: 2 }, "c")).toEqual(false);
4141
});
4242

43-
4443
// Given a value that isn't an object - an array, a string, a number,
4544
// null, or no argument at all
4645
// When passed to contains
4746
// Then it should throw Error("contains requires an object")
4847
// (careful: typeof [] and typeof null are both "object")
49-
test('contains passed invalid input like an array, a string or a number will throw an error', ()=>{
50-
expect(contains([5,"5",6],"a")).toThrow(new Error("contains require an object"));
51-
expect(contains('Ebra','Ebra')).toThrow(new Error("contains require an object"));
52-
expect(contains(1,1)).toThrow(new Error("contains require an object"));
48+
test("contains passed invalid input like an array, a string or a number will throw an error", () => {
49+
expect(() => contains([5, "5", 6], "a")).toThrow(
50+
new Error("contains require an object")
51+
);
52+
expect(() => contains("Ebra", "Ebra")).toThrow(
53+
new Error("contains require an object")
54+
);
55+
expect(() => contains(1, 1)).toThrow(new Error("contains require an object"));
5356
});

‎Sprint-2/implement/tally.js‎

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
function tally(array) {
2-
3-
if (!Array.isArray(array)){
4-
throw new TypeError('unexpected Array')
5-
}
6-
const charCount={};
7-
8-
9-
10-
for(const char of array){
11-
charCount[char] = (charCount[char] || 0) + 1;
12-
}
13-
return charCount
14-
15-
2+
if (!Array.isArray(array)) {
3+
throw new TypeError("tally requires an array");
4+
}
5+
const charCount = {};
6+
7+
for (const char of array) {
8+
charCount[char] = (charCount[char] || 0) + 1;
9+
}
10+
return charCount;
1611
}
1712

1813
module.exports = tally;

0 commit comments

Comments
 (0)