Skip to content

Commit 28de846

Browse files
committed
exercises solved excluding bonuses
1 parent 0f94b74 commit 28de846

File tree

2 files changed

+194
-111
lines changed

2 files changed

+194
-111
lines changed

src/functions-and-arrays.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,71 @@
11
// Iteration #1: Find the maximum
2+
function maxOfTwoNumbers(num1, num2) {
3+
return num1 > num2? num1 : num2
4+
}
25

36
// Iteration #2: Find longest word
47
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
58

9+
function findLongestWord(arr) {
10+
if (arr.length === 0) {
11+
return null;
12+
}
13+
let longestWord = '';
14+
for (let i = 0; i<arr.length; i++) {
15+
if (arr[i].length > longestWord.length) {
16+
longestWord = arr[i];
17+
}
18+
}
19+
20+
return longestWord;
21+
}
22+
623
// Iteration #3: Calculate the sum
724

825
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
926

27+
function sumNumbers(numbers) {
28+
let result = 0;
29+
for (let i = 0; i< numbers.length; i++) {
30+
result += numbers[i];
31+
}
32+
return result;
33+
}
34+
35+
//Iteration #3 Bonus: Calculate the sum of array elements
36+
37+
1038
// Iteration #4: Calculate the average
1139
// Level 1: Array of numbers
1240
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
1341

42+
function averageNumbers(arr) {
43+
if (arr.length === 0) {
44+
return null;
45+
}
46+
47+
let result = 0;
48+
for (let i = 0; i<arr.length; i++) {
49+
result += arr[i];
50+
}
51+
let average = result / arr.length;
52+
return average;
53+
}
54+
1455
// Level 2: Array of strings
1556
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
57+
function averageWordLength(arr) {
58+
if (arr.length === 0) {
59+
return null;
60+
}
61+
62+
let result = 0;
63+
for (let i = 0; i<arr.length; i++) {
64+
result += arr[i].length;
65+
}
66+
let average = result / arr.length;
67+
return average;
68+
}
1669

1770
// Iteration #5: Unique arrays
1871
const wordsUnique = [
@@ -29,9 +82,32 @@ const wordsUnique = [
2982
'bring'
3083
];
3184

85+
function uniquifyArray(arr) {
86+
if (arr.length === 0) {
87+
return null;
88+
} else if (arr.length === 1) {
89+
return arr;
90+
} else {
91+
let filteredArray = [];
92+
for (let i = 0; i<arr.length; i++) {
93+
if (filteredArray.indexOf(arr[i]) === -1) {
94+
filteredArray.push(arr[i]);
95+
}
96+
}
97+
return filteredArray;
98+
}
99+
}
100+
32101
// Iteration #6: Find elements
33102
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
34103

104+
function doesWordExist(list, word) {
105+
if (list.length === 0) {
106+
return null;
107+
}
108+
return list.includes(word)? true : false;
109+
}
110+
35111
// Iteration #7: Count repetition
36112
const wordsCount = [
37113
'machine',
@@ -47,6 +123,12 @@ const wordsCount = [
47123
'matter'
48124
];
49125

126+
function howManyTimes(wordArr, word) {
127+
let result = 0;
128+
wordArr.forEach( item => item === word? result++ : false)
129+
return result;
130+
}
131+
50132
// Iteration #8: Bonus
51133

52134
const matrix = [
@@ -71,3 +153,4 @@ const matrix = [
71153
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
72154
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
73155
];
156+

tests/functions-and-arrays.spec.js

Lines changed: 111 additions & 111 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-
});
88-
89-
it('should return zero if receives an empty array when called', () => {
90-
expect(sum([])).toBe(0);
91-
});
92-
93-
it('should return the sum with one number array', () => {
94-
expect(sum([4])).toBe(4);
95-
});
96-
97-
it('should return zero if all elements are zero', () => {
98-
expect(sum([0, 0, 0, 0, 0])).toBe(0);
99-
});
100-
101-
it('should return the sum when passed array of numbers', () => {
102-
expect(sum([10, 5, 4, 32, 8])).toBe(59);
103-
});
104-
105-
it('should return the sum when passed array of strings', () => {
106-
expect(sum(['ana', 'marco', 'nicolas', 'tania', 'ptwd'])).toBe(24);
107-
});
108-
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-
});
84+
// describe('Bonus: Calculate the sum', () => {
85+
// it('should create a function named sum', () => {
86+
// expect(typeof sum).toBe('function');
87+
// });
88+
89+
// it('should return zero if receives an empty array when called', () => {
90+
// expect(sum([])).toBe(0);
91+
// });
92+
93+
// it('should return the sum with one number array', () => {
94+
// expect(sum([4])).toBe(4);
95+
// });
96+
97+
// it('should return zero if all elements are zero', () => {
98+
// expect(sum([0, 0, 0, 0, 0])).toBe(0);
99+
// });
100+
101+
// it('should return the sum when passed array of numbers', () => {
102+
// expect(sum([10, 5, 4, 32, 8])).toBe(59);
103+
// });
104+
105+
// it('should return the sum when passed array of strings', () => {
106+
// expect(sum(['ana', 'marco', 'nicolas', 'tania', 'ptwd'])).toBe(24);
107+
// });
108+
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', () => {
@@ -164,22 +164,22 @@ describe('Calculate the average of an array of strings', () => {
164164
});
165165
});
166166

167-
describe('Bonus: Calculate the average of a mixed elements array', () => {
168-
it('should create a function named avg', () => {
169-
expect(typeof avg).toBe('function');
170-
});
167+
// describe('Bonus: Calculate the average of a mixed elements array', () => {
168+
// it('should create a function named avg', () => {
169+
// expect(typeof avg).toBe('function');
170+
// });
171171

172-
it('should return null if receives an empty array when called', () => {
173-
expect(avg([])).toBe(null);
174-
});
172+
// it('should return null if receives an empty array when called', () => {
173+
// expect(avg([])).toBe(null);
174+
// });
175175

176-
it('should return the average of the array', () => {
177-
// false is counted as 0
178-
expect(avg([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, false])).toBe(5.11);
179-
// true is counted as 1
180-
expect(avg([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, true])).toBe(5.22);
181-
});
182-
});
176+
// it('should return the average of the array', () => {
177+
// // false is counted as 0
178+
// expect(avg([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, false])).toBe(5.11);
179+
// // true is counted as 1
180+
// expect(avg([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, true])).toBe(5.22);
181+
// });
182+
// });
183183

184184
describe('Unique array', () => {
185185
it('should create a function named uniquifyArray', () => {
@@ -266,60 +266,60 @@ describe('Count repetition', () => {
266266
});
267267
});
268268

269-
describe('Bonus Quest - greatestProduct', () => {
270-
it('should create a function named greatestProduct', () => {
271-
expect(typeof greatestProduct).toBe('function');
272-
});
273-
274-
it('should return 1 (one) when all numbers of the arrays are 1', () => {
275-
let matrix = [
276-
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
277-
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
278-
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
279-
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
280-
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
281-
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
282-
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
283-
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
284-
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
285-
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
286-
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
287-
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
288-
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
289-
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
290-
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
291-
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
292-
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
293-
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
294-
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
295-
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
296-
];
297-
expect(greatestProduct(matrix)).toBe(1);
298-
});
299-
300-
it('should return 16 when all the numbers of the arrays are 2', () => {
301-
let matrix = [
302-
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
303-
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
304-
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
305-
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
306-
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
307-
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
308-
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
309-
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
310-
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
311-
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
312-
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
313-
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
314-
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
315-
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
316-
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
317-
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
318-
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
319-
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
320-
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
321-
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
322-
];
323-
expect(greatestProduct(matrix)).toBe(16);
324-
});
325-
});
269+
// describe('Bonus Quest - greatestProduct', () => {
270+
// it('should create a function named greatestProduct', () => {
271+
// expect(typeof greatestProduct).toBe('function');
272+
// });
273+
274+
// it('should return 1 (one) when all numbers of the arrays are 1', () => {
275+
// let matrix = [
276+
// [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
277+
// [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
278+
// [1, 1, 1, 1, 1,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// [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
295+
// [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
296+
// ];
297+
// expect(greatestProduct(matrix)).toBe(1);
298+
// });
299+
300+
// it('should return 16 when all the numbers of the arrays are 2', () => {
301+
// let matrix = [
302+
// [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
303+
// [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
304+
// [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
305+
// [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
306+
// [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
307+
// [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
308+
// [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
309+
// [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
310+
// [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
311+
// [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
312+
// [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
313+
// [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
314+
// [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
315+
// [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
316+
// [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
317+
// [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
318+
// [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
319+
// [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
320+
// [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
321+
// [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
322+
// ];
323+
// expect(greatestProduct(matrix)).toBe(16);
324+
// });
325+
// });

0 commit comments

Comments
 (0)