Skip to content

Commit 8da78e8

Browse files
committed
1 parent bbd5108 commit 8da78e8

File tree

1 file changed

+47
-12
lines changed

1 file changed

+47
-12
lines changed

src/functions-and-arrays.js

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,43 @@
11
// Iteration #1: Find the maximum
2-
function maxOfTwoNumbers() {}
3-
4-
5-
2+
function maxOfTwoNumbers(num1, num2) {
3+
if (num1 > num2){
4+
return num1;
5+
} else{
6+
return num2;
7+
}
8+
}
9+
maxOfTwoNumbers(3,2);
610
// Iteration #2: Find longest word
711
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
8-
9-
function findLongestWord() {}
12+
const arr = []
13+
function findLongestWord(banana) {
14+
// declare a variable store the longestWord
15+
let longestWord = "";
16+
// loop over the array
17+
for (let i=0; i<banana.length; i++){
18+
if (banana[i].length > longestWord.length){
19+
longestWord = banana[i]
20+
}
21+
return longestWord
22+
}
23+
}
24+
findLongestWord(words)
25+
findLongestWord(arr)
1026

1127

1228

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

16-
function sumNumbers() {}
17-
32+
function sumNumbers(arrayOfNumbers) {
33+
let result = 0;
34+
for(i = 0; i < arrayOfNumbers.length; i++){
35+
result += arrayOfNumbers[i];
36+
}
37+
return result;
38+
}
1839

40+
console.log(sumNumbers(numbers));
1941

2042
// Iteration #3.1 Bonus:
2143
function sum() {}
@@ -24,15 +46,28 @@ function sum() {}
2446

2547
// Iteration #4: Calculate the average
2648
// Level 1: Array of numbers
27-
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
49+
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];//6
2850

29-
function averageNumbers() {}
51+
function averageNumbers(array) {
52+
let average = sumNumbers(array) / array.length;
53+
return average;
54+
}
3055

56+
console.log(averageNumbers(numbersAvg));
3157

3258
// Level 2: Array of strings
33-
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
59+
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];//5,3
60+
61+
function averageWordLength(arrOfStr) {
62+
let sumOfChar = 0;
63+
for(j = 0; j < arrOfStr.length; j++){
64+
sumOfChar += arrOfStr[j].length;
65+
}
66+
let averageOfChar = sumOfChar / arrOfStr.length;
67+
return averageOfChar;
68+
}
3469

35-
function averageWordLength() { }
70+
console.log(averageWordLength(wordsArr));
3671

3772
// Bonus - Iteration #4.1
3873
function avg() {}

0 commit comments

Comments
 (0)