Skip to content

Commit 902f2eb

Browse files
committed
Trabalho concluido até Iteration ironhack-labs#7, feito por: Graziele Dantas e Jefferson Inácio WDFT
1 parent a6e71be commit 902f2eb

File tree

1 file changed

+173
-1
lines changed

1 file changed

+173
-1
lines changed

src/functions-and-arrays.js

Lines changed: 173 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,132 @@ function maxOfTwoNumbers(num1, num2) {
66
return num1
77
}
88
}
9+
maxOfTwoNumbers(5, 7)
10+
911

1012
// Iteration #2: Find longest word
1113
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
1214

13-
// Iteration #3: Calculate the sum
15+
function findLongestWord(words){
16+
let longest = "";
17+
if (words.length === 0){
18+
return null;
19+
} else{
20+
for (let i = 0; i < words.length; i++){
21+
if (longest.length < words[i].length){
22+
longest = words[i];
23+
}
24+
}
25+
}
26+
return longest;
27+
}
28+
29+
findLongestWord(words)
1430

31+
32+
// Iteration #3: Calculate the sum
1533
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
1634

35+
function sumNumbers(numbers){
36+
let total = 0;
37+
for (let i = 0; i < numbers.length; i++){
38+
total += numbers[i];
39+
}
40+
return total;
41+
}
42+
43+
sumNumbers(numbers);
44+
45+
46+
// Bonus - Iteration #3.1: A generic sum() function
47+
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
48+
49+
function sum(arr){
50+
let total = 0;
51+
for (let i = 0; i < arr.length; i++){
52+
if (typeof arr[i] === 'string'){
53+
total += arr[i].length;
54+
} else if (typeof arr[i] === 'boolean'){
55+
if (arr[i] === true){
56+
total += 1;
57+
}
58+
} else if (typeof arr[i] === 'number'){
59+
total += arr[i];
60+
} else if (typeof arr[i] === 'object' || typeof arr[i] === 'array'){
61+
throw new Error("Unsupported data type sir or ma'am");
62+
}
63+
}
64+
return total;
65+
}
66+
67+
sum(mixedArr);
68+
69+
1770
// Iteration #4: Calculate the average
1871
// Level 1: Array of numbers
1972
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
2073

74+
function averageNumbers(numbers){
75+
if (numbers.length === 0){
76+
return null;
77+
};
78+
79+
let total = 0;
80+
for (let i = 0; i < numbers.length; i++) {
81+
total += numbers[i];
82+
}
83+
84+
let average = total / numbers.length;
85+
return average;
86+
};
87+
88+
averageNumbers(numbersAvg);
2189
// Level 2: Array of strings
2290
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
2391

92+
function averageWordLength(words){
93+
if (words.length === 0) {
94+
return null;
95+
};
96+
97+
let total = 0;
98+
for (let i = 0; i < words.length; i++){
99+
total += words[i].length;
100+
}
101+
let average = total / words.length;
102+
return average;
103+
}
104+
averageWordLength(wordsArr)
105+
106+
107+
// Bonus - Iteration #4.1: A generic avg() function
108+
function avg(arr){
109+
if (arr.length === 0) {
110+
return null;
111+
};
112+
113+
let total = 0;
114+
for (let i = 0; i < arr.length; i++){
115+
if (typeof arr[i] === 'string'){
116+
total += arr[i].length;
117+
} else if (typeof arr[i] === 'boolean'){
118+
if (arr[i] === true){
119+
total += 1;
120+
}
121+
} else if (typeof arr[i] === 'number'){
122+
total += arr[i];
123+
}
124+
}
125+
let average = total / arr.length;
126+
average = average.toFixed(2)
127+
average = Number(average);
128+
129+
return average;
130+
}
131+
132+
avg(mixedArr);
133+
134+
24135
// Iteration #5: Unique arrays
25136
const wordsUnique = [
26137
'crab',
@@ -36,9 +147,47 @@ const wordsUnique = [
36147
'bring'
37148
];
38149

150+
function uniquifyArray(arr){
151+
if (arr.length === 0){
152+
return null;
153+
};
154+
155+
let unifiedArray = [];
156+
for (let i = 0; i < arr.length; i++){
157+
if (!unifiedArray.includes(arr[i])){
158+
unifiedArray.push(arr[i])
159+
}
160+
}
161+
return unifiedArray;
162+
}
163+
164+
uniquifyArray (wordsUnique);
165+
166+
39167
// Iteration #6: Find elements
40168
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
41169

170+
function doesWordExist(arr, palavraGoogleada){
171+
if (arr.length === 0){
172+
return null;
173+
};
174+
175+
let exists = false;
176+
for (let i = 0; i < arr.length; i++){
177+
if (arr[i] === palavraGoogleada){
178+
exists = true;
179+
}
180+
}
181+
if (exists) {
182+
return true;
183+
} else {
184+
return false;
185+
}
186+
};
187+
188+
doesWordExist(wordsFind, 'truth');
189+
190+
42191
// Iteration #7: Count repetition
43192
const wordsCount = [
44193
'machine',
@@ -54,6 +203,23 @@ const wordsCount = [
54203
'matter'
55204
];
56205

206+
function howManyTimes (arr, palavraGoogleada) {
207+
if (arr.length === 0) {
208+
return 0;
209+
};
210+
211+
let contador = 0;
212+
for (let i = 0; i < arr.length; i++) {
213+
if (palavraGoogleada === arr[i]) {
214+
contador++
215+
}
216+
}
217+
return contador;
218+
}
219+
220+
howManyTimes(wordsCount, 'matter');
221+
222+
57223
// Iteration #8: Bonus
58224

59225
const matrix = [
@@ -78,3 +244,9 @@ const matrix = [
78244
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
79245
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
80246
];
247+
248+
function greatestProduct(matrix) {
249+
250+
}
251+
var maxProduct = greatestProduct(matrix);
252+
console.log(maxProduct);

0 commit comments

Comments
 (0)