-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathlengthiestWord.js
More file actions
18 lines (18 loc) · 770 Bytes
/
lengthiestWord.js
File metadata and controls
18 lines (18 loc) · 770 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Write a function `lengthiestWord` that accepts a sentence string as an argument. The function should
// return the longest word of the sentence. If there is a tie, return the word that appears later
// in the sentence.
const lengthiestWord = (words) => {
let arrayWord = words.split(" ");
let lengthiest = arrayWord[0];
for (let i = 1; i < arrayWord.length; i++) {
let word = arrayWord[i];
if (lengthiest.length <= word.length) {
lengthiest = word;
}
}
return lengthiest;
};
console.log(lengthiestWord("I am pretty hungry")); // 'hungry'
console.log(lengthiestWord("we should think outside of the box")); // 'outside'
console.log(lengthiestWord("down the rabbit hole")); // 'rabbit'
console.log(lengthiestWord("simmer down")); // 'simmer'