This episode of Algorithm 101 features finding the longest word
. In how many ways can you do this?
We will be looking at 6 ways to Find the Longest Word in a Sentence or group of words.
longestWord("Njoku Samson Ebere"); // Samson
longestWord("Find the longest word"); // longest
Prerequisite
This article assumes that you have basic understanding of javascript's string and array methods.
Let's Find the Longest Word in a Sentence using:
- .split() and .sort()
function longestWord(sentence) {
return sentence.split(" ").sort((word, nextWord) => nextWord.length - word.length)[0];
}
- forEach(), if...statement and .split()
function longestWord(sentence) {
let splittedSentence = sentence.split(" ");
let maxLength = 0;
let maxWord = "";
splittedSentence.forEach(word => {
if (word.length > maxLength) {
maxLength = word.length;
maxWord = word;
}
});
return maxWord;
}
- map(), if...statement and .split()
function longestWord(sentence) {
let splittedSentence = sentence.split(" ");
let maxLength = 0;
let maxWord = "";
splittedSentence.map(word => {
if (word.length > maxLength) {
maxLength = word.length;
maxWord = word;
}
});
return maxWord;
}
- for...loop, if...statement and .split()
function longestWord(sentence) {
let splittedSentence = sentence.split(" ");
let maxLength = 0;
let maxWord = "";
for (let i = 0; i < splittedSentence.length; i++) {
if (splittedSentence[i].length > maxLength) {
maxLength = splittedSentence[i].length;
maxWord = splittedSentence[i];
}
}
return maxWord;
}
- for...of...loop, if...statement and .split()
function longestWord(sentence) {
let splittedSentence = sentence.split(" ");
let maxLength = 0;
let maxWord = "";
for (word of splittedSentence) {
if (word.length > maxLength) {
maxLength = word.length;
maxWord = word;
}
}
return maxWord;
}
- for...in...loop, if...statement and .split()
function longestWord(sentence) {
let splittedSentence = sentence.split(" ");
let maxLength = 0;
let maxWord = "";
for (word in splittedSentence) {
if (splittedSentence[word].length > maxLength) {
maxLength = splittedSentence[word].length;
maxWord = splittedSentence[word];
}
}
return maxWord;
}
Conclusion
There are many ways to solve problems programmatically. You are only limited by your imagination. This could also be achieved using .reduce()
method. Why not try it out and tell us how you did it in the comment section?
I will love to know other ways you solved yours in the comment section.
If you have questions, comments or suggestions, please drop them in the comment section.
You can also follow and message me on social media platforms.
Thank You For Your Time.
Top comments (8)
Here's the reduce version for those who are wondering:
Edited, to clarify how the code works:
sentence.split(" ")
returns an array of words.reduce
on it.currentWord
).This is awesome, Aleksandr. Works perfectly. Can you walk us through the code?
Sure, edited to clarify
Thank you for the explanation. You are good.
Hey !
I don't think you should use map like this. You use it like a forEach, and junior developer could think it's okay to use it like this while it's not :/
PamProg thank you for taking your time to read and comment. Can you explain further?
Well, your "forEach" and "map" examples are the same, so we could think that both are good to use. But the map (and I'm sure you know it) is used to make a new array, not to find something, filter something or whatever.
It's not because you can do it with map that you should use it and promote it :)
Thank you. You are awesome 😍