Skip to content

Commit 600f44b

Browse files
Iteration ironhack-labs#2: Find longest word
1 parent f1a508e commit 600f44b

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

starter-code/src/functions-and-arrays.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
11
// Iteration #1: Find the maximum
22

3+
function maxOfTwoNumbers(a, b){
4+
if (a > b) {
5+
return a;
6+
} else {
7+
return b;
8+
}
9+
}
10+
311
// Iteration #2: Find longest word
412
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
513

14+
function findLongestWord(arr) {
15+
let longestWord = null;
16+
if (arr.length > 0) {
17+
let wordLength = 0;
18+
for(var i = 0; i < arr.length; i++){
19+
if(arr[i].length > wordLength){
20+
wordLength = arr[i].length;
21+
longestWord = arr[i];
22+
}
23+
}
24+
}
25+
return longestWord;
26+
}
27+
28+
findLongestWord(words);
29+
30+
631
// Iteration #3: Calculate the sum
732

833
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
@@ -90,15 +115,4 @@ const matrix = [
90115
[20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16],
91116
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
92117
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
93-
];
94-
95-
96-
//Iteration #1: Find the maximum
97-
98-
function maxOfTwoNumbers(a, b){
99-
if (a > b) {
100-
return a;
101-
} else {
102-
return b;
103-
}
104-
}
118+
];

0 commit comments

Comments
 (0)