Skip to content

Commit e31df88

Browse files
committed
Iteration ironhack-labs#2: Find longest word almost Done
1 parent 5d3d7c2 commit e31df88

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/functions-and-arrays.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,26 @@ function maxOfTwoNumbers(number1, number2) {
1212
// Iteration #2: Find longest word
1313
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
1414

15-
function findLongestWord() {}
16-
15+
function findLongestWord(words) {
16+
let longestWord = '';
17+
let longestWordNumber = 0;
18+
if (words.length === 1 || typeof words === "string") {
19+
return words[0]
20+
}
21+
else if (words.length === 0 || typeof words !== "object" ) {
22+
return null
23+
} else {
24+
for (let i=0; i < words.length - 1; i++){
25+
if(words[i].length === words[i + 1].length) {
26+
return words[i]
27+
} else if (words[i].length > words[i + 1].length && words[i].length > longestWordNumber) {
28+
let longestWordNumber = words[i].length;
29+
let longestWord = words[i];
30+
return longestWord
31+
}
32+
}
33+
}
34+
}
1735

1836

1937
// Iteration #3: Calculate the sum

0 commit comments

Comments
 (0)