Skip to content

Commit b8a92fa

Browse files
committed
all done but ironhack-labs#7
1 parent bbd5108 commit b8a92fa

File tree

1 file changed

+80
-28
lines changed

1 file changed

+80
-28
lines changed

src/functions-and-arrays.js

Lines changed: 80 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,75 @@
11
// Iteration #1: Find the maximum
2-
function maxOfTwoNumbers() {}
3-
4-
5-
2+
function maxOfTwoNumbers(a, b) {
3+
if (a > b) {
4+
return a;
5+
} else if (b > a) {
6+
return b;
7+
} else if (a === b) {
8+
return a && b;
9+
}
10+
}
611
// Iteration #2: Find longest word
712
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
813

9-
function findLongestWord() {}
10-
11-
14+
function findLongestWord(words) {
15+
let hugeWord = '';
16+
if (words.length === 0) {
17+
return null;
18+
} else if (words.length === 1) {
19+
return words[0];
20+
} else {
21+
for (let i = 0; i < words.length; i++) {
22+
if (words[i].length > hugeWord.length) {
23+
hugeWord = words[i];
24+
}
25+
}
26+
}
27+
return hugeWord;
28+
}
1229

1330
// Iteration #3: Calculate the sum
1431
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
1532

16-
function sumNumbers() {}
17-
18-
33+
function sumNumbers(numbers) {
34+
let totalSum = 0;
35+
for (let i = 0; i < numbers.length; i++) {
36+
totalSum += numbers[i];
37+
}
38+
return totalSum;
39+
}
1940

2041
// Iteration #3.1 Bonus:
2142
function sum() {}
2243

23-
24-
2544
// Iteration #4: Calculate the average
2645
// Level 1: Array of numbers
2746
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
2847

29-
function averageNumbers() {}
48+
function averageNumbers(numbersAvg) {
49+
if (numbersAvg.length === 0) {
50+
return null;
51+
}
3052

53+
let totalSum = 0;
54+
for (let i = 0; i < numbersAvg.length; i++) {
55+
totalSum += numbersAvg[i];
56+
}
57+
return totalSum / numbersAvg.length;
58+
}
3159

3260
// Level 2: Array of strings
3361
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
3462

35-
function averageWordLength() { }
63+
function averageWordLength(wordsArr) {
64+
if (wordsArr.length === 0) {
65+
return null;
66+
}
67+
let totalSumOfLetters = 0;
68+
for (let i = 0; i < wordsArr.length; i++) {
69+
totalSumOfLetters += wordsArr[i].length;
70+
}
71+
return totalSumOfLetters / wordsArr.length;
72+
}
3673

3774
// Bonus - Iteration #4.1
3875
function avg() {}
@@ -52,16 +89,27 @@ const wordsUnique = [
5289
'bring'
5390
];
5491

55-
function uniquifyArray() {}
56-
57-
92+
function uniquifyArray(wordsUnique) {
93+
if (wordsUnique.length === 0) {
94+
return null;
95+
}
96+
}
5897

5998
// Iteration #6: Find elements
6099
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
61-
62-
function doesWordExist() {}
63-
64-
100+
let givenArr;
101+
function doesWordExist(wordsFind, givenArr) {
102+
for (let i = 0; i < wordsFind.length; i++) {
103+
if (givenArr === wordsFind[i]) {
104+
return true;
105+
}
106+
}
107+
if (wordsFind.length === 0) {
108+
return null;
109+
} else {
110+
return false;
111+
}
112+
}
65113

66114
// Iteration #7: Count repetition
67115
const wordsCount = [
@@ -77,10 +125,17 @@ const wordsCount = [
77125
'disobedience',
78126
'matter'
79127
];
80-
81-
function howManyTimes() {}
82-
83-
128+
let oneWordToSearch;
129+
130+
function howManyTimes(wordsCount, oneWordToSearch) {
131+
let counterOfWords = 0;
132+
for (let i = 0; i < wordsCount.length; i++) {
133+
if (oneWordToSearch === wordsCount[i]) {
134+
counterOfWords++;
135+
}
136+
}
137+
return counterOfWords;
138+
}
84139

85140
// Iteration #8: Bonus
86141
const matrix = [
@@ -108,9 +163,6 @@ const matrix = [
108163

109164
function greatestProduct() {}
110165

111-
112-
113-
114166
// The following is required to make unit tests work.
115167
/* Environment setup. Do not modify the below code. */
116168
if (typeof module !== 'undefined') {

0 commit comments

Comments
 (0)