Skip to content

Commit c02cd0e

Browse files
committed
Iteration 2, 3, 3.1 complete
1 parent ed64dca commit c02cd0e

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

src/functions-and-arrays.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// Iteration #1: Find the maximum
22
function maxOfTwoNumbers(num1, num2) {
33
if (num1 > num2) {
4-
maxNumber = num1;
4+
return num1;
55
} else {
6-
maxNumber = num2;
6+
return num2;
77
}
8-
return maxNumber;
98
}
109
console.log(maxOfTwoNumbers(5, 3));
1110

@@ -14,7 +13,21 @@ console.log(maxOfTwoNumbers(5, 3));
1413
// Iteration #2: Find longest word
1514
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
1615

17-
function findLongestWord() {}
16+
function findLongestWord(array) {
17+
let word = '';
18+
if (array.length === 0) {
19+
return null;
20+
}
21+
for (let i = 0; i < words.length; i++) {
22+
if (word.length < array[i].length) {
23+
word = array[i];
24+
}
25+
}
26+
return word;
27+
}
28+
29+
console.log(findLongestWord(words));
30+
1831

1932

2033

@@ -31,10 +44,19 @@ function sumNumbers(numbers) {
3144

3245

3346
// Iteration #3.1 Bonus:
34-
function sum(arr) {
47+
function sum(array) {
48+
let sumArray = 0; //initialize variable for our sum number to store
3549

50+
// Using for loop, iterate through an array; as we do not know how many values inside an array, we need to use array.length;
51+
// code inside the loop is
52+
for (let i = 0; i < array.length; i++) {
53+
sumArray += array[i];
54+
}
55+
return sumArray;
3656
}
3757

58+
console.log(sum([5, 6, 5]));
59+
3860

3961

4062
// Iteration #4: Calculate the average

0 commit comments

Comments
 (0)