Skip to content

Commit 79e8262

Browse files
committed
done iteration ironhack-labs#2
1 parent 87e2c64 commit 79e8262

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/functions-and-arrays.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,24 @@ function maxOfTwoNumbers(numA, numB) {
1616
// Iteration #2: Find longest word
1717
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
1818

19-
function findLongestWord() {}
19+
function findLongestWord(words)
20+
{
21+
if (words.length === 0) { //returns null if array is empty
22+
return null
23+
}
24+
25+
if (words.length === 1) { //returns first word if array has only 1 word
26+
return words[0]
27+
}
28+
29+
let longestWord = "" // returns longest word, first occurence
30+
for (let word of words) {
31+
if (longestWord.length < word.length) {
32+
longestWord = word
33+
}
34+
}
35+
return longestWord
36+
}
2037

2138

2239

0 commit comments

Comments
 (0)