Algorithm Find The Longest Word In A String - thelastmile/FreeCodeCamp GitHub Wiki
Algorithm Find the Longest Word in a String
:triangular_flag_on_post: Remember to use Read-Search-Ask
if you get stuck. Try to pair program :busts_in_silhouette: and write your own code :pencil:
:checkered_flag: Problem Explanation:
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.
Relevant Links
:speech_balloon: Hint: 1
You should split the string into an array of words.
try to solve the problem now
:speech_balloon: Hint: 2
You will need to figure out a way to keep track globally of the greatest current length.
try to solve the problem now
:speech_balloon: Hint: 3
Remember how to get the length of elements on the array? Array[index].length
.
try to solve the problem now
Spoiler Alert!
Solution ahead!
:beginner: Basic Code Solution:
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;
}
:rocket: Run Code
Code Explanation:
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.
Relevant Links
:sunflower: Intermediate Code Solution:
Using .reduce()
function findLongestWord(s) {
return s.split(' ')
.reduce(function(x, y) {
return Math.max(x, y.length)
}, 0);
}
:rocket: Run Code
Code Explanation:
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.
Relevant Links
:rotating_light: Advanced Code Solution:
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(" "));
}
}
:rocket: Run Code
Code Explanation:
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.
Relevant Links
:trophy: Credits:
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
:clipboard: NOTES FOR CONTRIBUTIONS:
- :warning: 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. :traffic_light:
- Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)
See :point_right:
Wiki Challenge Solution Template
for reference.