Skip to content

Commit c18234f

Browse files
committed
reiterate on the bonus iteration for calculatin the sum of mixed array
1 parent 34b674f commit c18234f

File tree

2 files changed

+75
-57
lines changed

2 files changed

+75
-57
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,12 @@ You can use the following array to test your solution:
107107
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
108108
```
109109

110-
### Bonus - Iteration #3.1: Calculate the sum for array filled with (_almost_) any type of data
110+
### Bonus - Iteration #3.1: A generic `sum()` function
111111

112-
**The goal: Learn how to refactor your**
113-
In the iteration 3, you created a function that will return a sum of array of numbers. But what if we want to know how much is a sum of some array of words? We wouldn't be able to use the same function as above, or better saying, we would have to _tweak_ it a little bit so it can be reused no matter what is in the array that is passed as argument when function `sumArray` is called.
112+
**The goal: Learn how to refactor your code.** :muscle:
113+
In the iteration 3, you created a function that will return a sum of array of numbers. But what if we want to know how much is a sum of some array of words? We wouldn't be able to use the same function as above, or better saying, we would have to _tweak_ it a little bit so it can be reused no matter what is in the array that is passed as argument when function _sumArray()_ is called.
114+
115+
Let's create a new function `sum()` that calculates the sum for array filled with (_almost_) any type of data. Check the tests for more details.
114116

115117
You can use the following array to test your solution:
116118

@@ -151,7 +153,13 @@ const words = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart',
151153

152154
### Bonus - Iteration #4.1: A generic `avg()` function
153155

154-
Create function `avg(arr)` that receive any array filled with numbers and/or strings and calculates average.
156+
Create function `avg(arr)` that receives any mixed array and calculates average. Consider as mixed array an array filled with numbers and/or strings and/or booleans.
157+
158+
```javascript
159+
const mixedArr = [6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, 10];
160+
161+
// should return: 6.22
162+
```
155163

156164
## Iteration #5: Unique arrays
157165

starter-code/tests/functions-and-arrays.spec.js

Lines changed: 63 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -57,52 +57,68 @@ describe('Find the longest word', () => {
5757
});
5858
});
5959

60-
describe('Calculate the sum', () => {
61-
it('should create a function named sumArray', () => {
62-
expect(typeof sumArray).toBe('function');
60+
describe('Calculate the sum of array of numbers', () => {
61+
it('should create a function named sumNumbers', () => {
62+
expect(typeof sumNumbers).toBe('function');
6363
});
6464

6565
it('should return zero if receives an empty array when called', () => {
66-
expect(sumArray([])).toBe(0);
66+
expect(sumNumbers([])).toBe(0);
6767
});
6868

6969
it('should return the sum with one number array', () => {
70-
expect(sumArray([4])).toBe(4);
70+
expect(sumNumbers([4])).toBe(4);
7171
});
7272

7373
it('should return zero if all elements are zero', () => {
74-
expect(sumArray([0, 0, 0, 0, 0])).toBe(0);
74+
expect(sumNumbers([0, 0, 0, 0, 0])).toBe(0);
7575
});
7676

7777
it('should return the sum when passed array of numbers', () => {
78-
expect(sumArray([10, 5, 4, 32, 8])).toBe(59);
78+
expect(sumNumbers([10, 5, 4, 32, 8])).toBe(59);
7979
});
80+
});
8081

81-
it('bonus - should return the sum when passed array of strings', () => {
82-
expect(sumArray(['ana', 'marco', 'nicolas', 'tania', 'ptwd'])).toBe(24);
82+
describe('Bonus: Calculate the sum', () => {
83+
it('should create a function named sum', () => {
84+
expect(typeof sum).toBe('function');
8385
});
8486

85-
it('bonus - should return the sum when passed array of mixed strings and numbers - ', () => {
86-
expect(sumArray([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, 10])).toBe(
87-
56
88-
);
87+
it('should return zero if receives an empty array when called', () => {
88+
expect(sum([])).toBe(0);
89+
});
90+
91+
it('should return the sum with one number array', () => {
92+
expect(sum([4])).toBe(4);
93+
});
94+
95+
it('should return zero if all elements are zero', () => {
96+
expect(sum([0, 0, 0, 0, 0])).toBe(0);
8997
});
90-
it('bonus - should return the sum when passed array of mixed strings, numbers and booleans - ', () => {
98+
99+
it('should return the sum when passed array of numbers', () => {
100+
expect(sum([10, 5, 4, 32, 8])).toBe(59);
101+
});
102+
103+
it('should return the sum when passed array of strings', () => {
104+
expect(sum(['ana', 'marco', 'nicolas', 'tania', 'ptwd'])).toBe(24);
105+
});
106+
107+
it('should return the sum when passed array of mixed strings and numbers - ', () => {
108+
expect(sum([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, 10])).toBe(56);
109+
});
110+
it('should return the sum when passed array of mixed strings, numbers and booleans - ', () => {
91111
// false is counted as 0
92-
expect(
93-
sumArray([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, false])
94-
).toBe(46);
112+
expect(sum([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, false])).toBe(46);
95113
// true is counted as 1
96-
expect(
97-
sumArray([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, true])
98-
).toBe(47);
114+
expect(sum([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, true])).toBe(47);
99115
});
100116
it('should throw an error when unsupported data type (object or array) present in the array', () => {
101117
// const arr = [6, 12, "miami", 1, "barca", "200", "lisboa", 8, [], {}];
102118

103-
expect(() =>
104-
sumArray([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, [], {}])
105-
).toThrow(new Error("Unsupported data type sir or ma'am"));
119+
expect(() => sum([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, [], {}])).toThrow(
120+
new Error("Unsupported data type sir or ma'am")
121+
);
106122
});
107123
});
108124

@@ -112,6 +128,7 @@ describe('Calculate the average of an array of numbers', () => {
112128
});
113129

114130
it('should return null if receives an empty array when called', () => {
131+
// should it return null or zero?
115132
expect(averageNumbers([])).toBe(null);
116133
});
117134

@@ -136,6 +153,7 @@ describe('Calculate the average of an array of strings', () => {
136153
});
137154

138155
it('should return null if receives an empty array when called', () => {
156+
// should it return null or zero?
139157
expect(averageWordLength([])).toBe(null);
140158
});
141159

@@ -146,20 +164,29 @@ describe('Calculate the average of an array of strings', () => {
146164

147165
it('should return the average of a the array', () => {
148166
expect(
149-
averageWordLength([
150-
'Ironhack',
151-
'Madrid',
152-
'Barcelona',
153-
'Paris',
154-
'Miami',
155-
'Mexico',
156-
'Berlin',
157-
'Programmers'
158-
])
167+
averageWordLength(['Ironhack', 'Madrid', 'Barcelona', 'Paris', 'Miami', 'Mexico', 'Berlin', 'Programmers'])
159168
).toBe(7);
160169
});
161170
});
162171

172+
describe('Bonus: Calculate the average of a mixed elements array', () => {
173+
it('should create a function named avg', () => {
174+
expect(typeof avg).toBe('function');
175+
});
176+
177+
it('should return null if receives an empty array when called', () => {
178+
// should it return null or zero?
179+
expect(avg([])).toBe(null);
180+
});
181+
182+
it('should return the average of the array', () => {
183+
// false is counted as 0
184+
expect(avg([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, false])).toBe(5.11);
185+
// true is counted as 1
186+
expect(avg([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, true])).toBe(5.22);
187+
});
188+
});
189+
163190
describe('Unique array', () => {
164191
it('should create a function named uniquifyArray', () => {
165192
expect(typeof uniquifyArray).toBe('function');
@@ -183,17 +210,7 @@ describe('Unique array', () => {
183210

184211
it('should return the uniquified array', () => {
185212
expect(
186-
uniquifyArray([
187-
'iPhone',
188-
'Samsung',
189-
'Android',
190-
'iOS',
191-
'iPhone',
192-
'Samsung',
193-
'Nokia',
194-
'Blackberry',
195-
'Android'
196-
])
213+
uniquifyArray(['iPhone', 'Samsung', 'Android', 'iOS', 'iPhone', 'Samsung', 'Nokia', 'Blackberry', 'Android'])
197214
).toEqual(['iPhone', 'Samsung', 'Android', 'iOS', 'Nokia', 'Blackberry']);
198215
});
199216
});
@@ -223,12 +240,7 @@ describe('Find elements', () => {
223240
// });
224241

225242
it('should return true if the word we are looking for is in the array', () => {
226-
expect(
227-
doesWordExist(
228-
['pizza', 'sandwich', 'snack', 'soda', 'book', 'computer'],
229-
'book'
230-
)
231-
).toBe(true);
243+
expect(doesWordExist(['pizza', 'sandwich', 'snack', 'soda', 'book', 'computer'], 'book')).toBe(true);
232244
});
233245
});
234246

@@ -242,9 +254,7 @@ describe('Count repetition', () => {
242254
});
243255

244256
it('should return 1 (one) when the word appears only one time in the array', () => {
245-
expect(howManyTimes(['basketball', 'football', 'tennis'], 'tennis')).toBe(
246-
1
247-
);
257+
expect(howManyTimes(['basketball', 'football', 'tennis'], 'tennis')).toBe(1);
248258
});
249259

250260
it("should return 0 (zero) when the word doesn't appear in the array", () => {

0 commit comments

Comments
 (0)