Skip to content

Commit 496b82c

Browse files
committed
done 3.1
1 parent 93f2b9f commit 496b82c

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/functions-and-arrays.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,37 @@ const words = [
1515
];
1616

1717
function findLongestWord(words) {
18-
return words.reduce((longest, current) => {
19-
return current.length > longest.length ? current : longest;
20-
}, '');
18+
//check if the array is empty
19+
if (words.length === 0) {
20+
return null;
21+
}
22+
//initialize longest with the first element of the array
23+
let longest = words[0];
24+
//iterate through the array from the second word
25+
for (let i = 1; i < words.length; i++) {
26+
if (words[i].length > longest.length) {
27+
//here is where longest gets updated
28+
longest = words[i];
29+
}
30+
}
31+
return longest;
2132
}
2233

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

26-
function sumNumbers() {}
37+
function sumNumbers(numbers) {
38+
let sum = 0;
39+
for (let i = 0; i < numbers.length; i++) {
40+
sum += numbers[i];
41+
}
42+
return sum;
43+
}
2744

2845
// Iteration #3.1 Bonus:
29-
function sum() {}
46+
function sum() {
47+
48+
}
3049

3150
// Iteration #4: Calculate the average
3251
// Level 1: Array of numbers

0 commit comments

Comments
 (0)