Algorithm Find The Longest Word In A String - ashish9342/FreeCodeCamp GitHub Wiki
🚩 Remember to use Read-Search-Ask
if you get stuck. Try to pair program 👥 and write your own code 📝
You have to go through each word and figure out which one is the longest and return not the word, but how many characters it has.
You should split the string into an array of words.
try to solve the problem now
You will need to figure out a way to keep track globally of the greatest current length.
try to solve the problem now
Remember how to get the length of elements on the array? Array[index].length
.
try to solve the problem now
Solution ahead!
function findLongestWord(str) {
var words = str.split(' ');
var maxLength = 0;
for (var i = 0; i < words.length; i++) {
if (words[i].length > maxLength) {
maxLength = words[i].length;
}
}
return maxLength;
}
🚀 Run Code
Take the string and convert it into an array of words. Declare a variable to keep track of the maximum length and loop from 0 to the length of the array of words.
Then check for the longest word by comparing the current word to the previous one and storing the new longest word. At the end of the loop just return the number value of the variable maxLength.
Using .reduce()
function findLongestWord(s) {
return s.split(' ')
.reduce(function(x, y) {
return Math.max(x, y.length)
}, 0);
}
🚀 Run Code
For more information on reduce
click here.
In case you're wondering about that 0
after the callback function, it is used to give an initial value to the x
, so that Math.max
will know where to start.
Using recursiveness
function findLongestWord(str) {
//split the string into individual words
//(important!!, you'll see why later)
str = str.split(" ");
//str only has 1 element left that is the longest element,
//return the length of that element
if(str.length == 1){
return str[0].length;
}
//if the first element's length is greater than the second element's (or equal)
//remove the second element and recursively call the function)
if(str[0].length >= str[1].length){
str.splice(1,1);
return findLongestWord(str.join(" "));
}
//if the second element's length is greater thant the first element's start
//call the function past the first element
if(str[0].length <= str[1].length){
// from the first element to the last element inclusive.
return findLongestWord(str.slice(1,str.length).join(" "));
}
}
🚀 Run Code
The first line splits the string into individual words. Then we check if str
only has 1 element left then that is the longest element and we return it. If the first element's length is greater than the second element's (or equal), we remove the second element and recursively call the function findLongestWord
. However, if the second element's length is greater thant the first element's start, then we call the function past the first element.
If you found this page useful, you can give thanks by copying and pasting this on the main chat:
Thanks @Rafase282 @shadowfool @Hallaathrad for your help with Algorithm: Find the Longest Word in a String
⚠️ DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.- Add an explanation of your solution.
- Categorize the solution in one of the following categories — Basic, Intermediate and Advanced. 🚥
- Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)
See 👉
Wiki Challenge Solution Template
for reference.