Skip to content

Commit 0098014

Browse files
committed
solves exercise ironhack-labs#3 Bonus
1 parent 28de846 commit 0098014

File tree

2 files changed

+52
-35
lines changed

2 files changed

+52
-35
lines changed

src/functions-and-arrays.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,24 @@ function sumNumbers(numbers) {
3333
}
3434

3535
//Iteration #3 Bonus: Calculate the sum of array elements
36-
36+
function sum(elements) {
37+
let result = 0;
38+
elements.forEach( function(item) {
39+
switch(typeof item) {
40+
case 'string':
41+
result += item.length;
42+
break;
43+
case 'number':
44+
case 'boolean':
45+
result += Number(item);
46+
break;
47+
case 'object':
48+
case 'array':
49+
throw new Error("Unsupported data type sir or ma'am");
50+
}
51+
});
52+
return result;
53+
}
3754

3855
// Iteration #4: Calculate the average
3956
// Level 1: Array of numbers

tests/functions-and-arrays.spec.js

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -81,46 +81,46 @@ describe('Calculate the sum of array of numbers', () => {
8181
});
8282
});
8383

84-
// describe('Bonus: Calculate the sum', () => {
85-
// it('should create a function named sum', () => {
86-
// expect(typeof sum).toBe('function');
87-
// });
84+
describe('Bonus: Calculate the sum', () => {
85+
it('should create a function named sum', () => {
86+
expect(typeof sum).toBe('function');
87+
});
8888

89-
// it('should return zero if receives an empty array when called', () => {
90-
// expect(sum([])).toBe(0);
91-
// });
89+
it('should return zero if receives an empty array when called', () => {
90+
expect(sum([])).toBe(0);
91+
});
9292

93-
// it('should return the sum with one number array', () => {
94-
// expect(sum([4])).toBe(4);
95-
// });
93+
it('should return the sum with one number array', () => {
94+
expect(sum([4])).toBe(4);
95+
});
9696

97-
// it('should return zero if all elements are zero', () => {
98-
// expect(sum([0, 0, 0, 0, 0])).toBe(0);
99-
// });
97+
it('should return zero if all elements are zero', () => {
98+
expect(sum([0, 0, 0, 0, 0])).toBe(0);
99+
});
100100

101-
// it('should return the sum when passed array of numbers', () => {
102-
// expect(sum([10, 5, 4, 32, 8])).toBe(59);
103-
// });
101+
it('should return the sum when passed array of numbers', () => {
102+
expect(sum([10, 5, 4, 32, 8])).toBe(59);
103+
});
104104

105-
// it('should return the sum when passed array of strings', () => {
106-
// expect(sum(['ana', 'marco', 'nicolas', 'tania', 'ptwd'])).toBe(24);
107-
// });
105+
it('should return the sum when passed array of strings', () => {
106+
expect(sum(['ana', 'marco', 'nicolas', 'tania', 'ptwd'])).toBe(24);
107+
});
108108

109-
// it('should return the sum when passed array of mixed strings and numbers - ', () => {
110-
// expect(sum([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, 10])).toBe(56);
111-
// });
112-
// it('should return the sum when passed array of mixed strings, numbers and booleans - ', () => {
113-
// // false is counted as 0
114-
// expect(sum([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, false])).toBe(46);
115-
// // true is counted as 1
116-
// expect(sum([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, true])).toBe(47);
117-
// });
118-
// it('should throw an error when unsupported data type (object or array) present in the array', () => {
119-
// expect(() => sum([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, [], {}])).toThrow(
120-
// new Error("Unsupported data type sir or ma'am")
121-
// );
122-
// });
123-
// });
109+
it('should return the sum when passed array of mixed strings and numbers - ', () => {
110+
expect(sum([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, 10])).toBe(56);
111+
});
112+
it('should return the sum when passed array of mixed strings, numbers and booleans - ', () => {
113+
// false is counted as 0
114+
expect(sum([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, false])).toBe(46);
115+
// true is counted as 1
116+
expect(sum([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, true])).toBe(47);
117+
});
118+
it('should throw an error when unsupported data type (object or array) present in the array', () => {
119+
expect(() => sum([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, [], {}])).toThrow(
120+
new Error("Unsupported data type sir or ma'am")
121+
);
122+
});
123+
});
124124

125125
describe('Calculate the average of an array of numbers', () => {
126126
it('should create a function named averageNumbers', () => {

0 commit comments

Comments
 (0)