Skip to content

Commit 5bfe7b9

Browse files
committed
add 'Iteration ironhack-labs#1: Find the maximum', 'Iteration ironhack-labs#2: Find the longest word', 'Iteration ironhack-labs#3: Calculate the sum', 'Iteration ironhack-labs#4: Calculate the average', 'Iteration ironhack-labs#5: Unique arrays', 'Iteration ironhack-labs#6: Find elements' and 'Iteration ironhack-labs#7: Count repetition'
1 parent cb981d0 commit 5bfe7b9

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

src/functions-and-arrays.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,75 @@
11
// Iteration #1: Find the maximum
22

3+
function maxOfTwoNumbers(num1, num2) {
4+
if (num1 > num2) {
5+
return console.log(`O maior número é ${num1}`);
6+
}
7+
return console.log(`O maior número é ${num2}`);
8+
}
9+
10+
maxOfTwoNumbers(1, 3);
311

412
// Iteration #2: Find longest word
513
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
14+
let longestWord = '';
15+
16+
function findLongestWord(str) {
17+
18+
for (i = 0; i < str.length; i++) {
19+
if (str[i].length > longestWord) {
20+
longestWord = str[i].length;
21+
}
22+
}
23+
return `A maior palavra possui ${longestWord} letras`;
24+
}
25+
26+
console.log(findLongestWord(words));
27+
28+
629

730
// Iteration #3: Calculate the sum
831

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

34+
function sumNumbers(sum) {
35+
let total = 0;
36+
sum.forEach(function (element) {
37+
total += element;
38+
});
39+
return total;
40+
}
41+
42+
console.log(`A soma total dos números é ${sumNumbers(numbers)}`);
43+
44+
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
45+
46+
47+
1148
// Iteration #4: Calculate the average
1249
// Level 1: Array of numbers
1350
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
1451

52+
53+
function averageNumbers(number){
54+
if(!number.length) return;
55+
56+
return sumNumbers(number) / number.length;
57+
}
58+
59+
console.log(`A soma dos números é ${sumNumbers(numbersAvg)}`);
60+
console.log(`A média dos números é ${averageNumbers(numbersAvg)}`);
61+
62+
1563
// Level 2: Array of strings
1664
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
1765

66+
function averageWordLength(str){
67+
68+
return str.join('').length / str.length;
69+
}
70+
71+
console.log(`A média do comprimento das palavras é ${averageWordLength(wordsArr)}`)
72+
1873
// Iteration #5: Unique arrays
1974
const wordsUnique = [
2075
'crab',
@@ -30,9 +85,17 @@ const wordsUnique = [
3085
'bring'
3186
];
3287

88+
console.log(wordsUnique.filter((item, index) => wordsUnique.indexOf(item) === index))
89+
3390
// Iteration #6: Find elements
3491
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
3592

93+
function doesWordExist(arrayWords, wordSearch){
94+
return arrayWords.includes(wordSearch);
95+
}
96+
97+
console.log(doesWordExist(wordsFind, 'subset'))
98+
3699
// Iteration #7: Count repetition
37100
const wordsCount = [
38101
'machine',
@@ -48,6 +111,13 @@ const wordsCount = [
48111
'matter'
49112
];
50113

114+
function howManyTimes(arrayWords, wordSearch){
115+
116+
return arrayWords.filter( element => element === wordSearch).length
117+
}
118+
119+
console.log(howManyTimes(wordsCount, 'matter'))
120+
51121
// Iteration #8: Bonus
52122

53123
const matrix = [

0 commit comments

Comments
 (0)