Skip to content

Commit 64103e6

Browse files
authored
Merge pull request #1 from Andybetan/develop
lab js funtions terminado
2 parents 8ff766d + 81e82ac commit 64103e6

File tree

1 file changed

+98
-9
lines changed

1 file changed

+98
-9
lines changed

src/functions-and-arrays.js

Lines changed: 98 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,50 @@
11
// Iteration #1: Find the maximum
2-
function maxOfTwoNumbers() {}
3-
2+
function maxOfTwoNumbers(NumOne, NumTwo) {
3+
if (NumOne > NumTwo) {
4+
return NumOne;
5+
} else { return NumTwo;
6+
}
7+
}
8+
9+
410

511

612
// Iteration #2: Find longest word
713
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
814

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

1130

1231

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

16-
function sumNumbers() {}
35+
function sumNumbers(numbers) {
36+
if (numbers.length === 0){
37+
return 0;
38+
}
39+
let sum = 0;
40+
41+
for (let i = 0; i < numbers.length; i++) {
42+
sum += numbers [i];
43+
}
44+
return sum;
45+
}
46+
47+
1748

1849

1950

@@ -26,13 +57,34 @@ function sum() {}
2657
// Level 1: Array of numbers
2758
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
2859

29-
function averageNumbers() {}
60+
function averageNumbers(numbers) {
61+
if (numbers.length === 0) {
62+
return null;
63+
}
64+
const sum = numbers.reduce ((acc, num) =>
65+
acc + num, 0);
66+
const average = sum / numbers.length;
67+
68+
return average;
69+
}
70+
3071

3172

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

35-
function averageWordLength() { }
76+
function averageWordLength(words) {
77+
if (words.length === 0) {
78+
return null;
79+
}
80+
const totalLength = words.reduce((acc,word) => acc + word.length, 0);
81+
const averageLength = totalLength /
82+
words.length;
83+
84+
return averageLength;
85+
}
86+
87+
3688

3789
// Bonus - Iteration #4.1
3890
function avg() {}
@@ -52,14 +104,39 @@ const wordsUnique = [
52104
'bring'
53105
];
54106

55-
function uniquifyArray() {}
107+
function uniquifyArray(array) {
108+
if ( array.length === 0) {
109+
return null;
110+
}
111+
const uniqueArray = [];
112+
array.forEach((element) => {
113+
if (uniqueArray.indexOf(element) === -1)
114+
{
115+
uniqueArray.push(element);
116+
}
117+
});
118+
119+
return uniqueArray;
120+
}
56121

57122

58123

59124
// Iteration #6: Find elements
60125
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
61126

62-
function doesWordExist() {}
127+
function doesWordExist(wordArray,
128+
targetWord) {
129+
if (wordArray.length === 0) {
130+
return null;
131+
}
132+
for (let i = 0; i < wordArray.length; i++)
133+
{
134+
if (wordArray[i] === targetWord) {
135+
return true;
136+
}
137+
}
138+
return false;
139+
}
63140

64141

65142

@@ -78,7 +155,19 @@ const wordsCount = [
78155
'matter'
79156
];
80157

81-
function howManyTimes() {}
158+
function howManyTimes(wordArray, targetWord) {
159+
if (wordArray.length === 0) {
160+
return 0;
161+
}
162+
let count = 0;
163+
for (let i = 0; i < wordArray.length; i++)
164+
{
165+
if(wordArray[i] === targetWord) {
166+
count++;
167+
}
168+
}
169+
return count;
170+
}
82171

83172

84173

0 commit comments

Comments
 (0)