freeCodeCamp Challenge Guide: Find the Longest Word in a String

This is a really basic solution here

also its a general better practice to use let as its a smallerscoped than var

``````function findLongestWord(str) {

//Convert to array to seperate words
let Arr = str.split(' ');

//Store first word as current longest
let curLongest = Arr[0];

//Compare word lengths
for (let i=0; i<Arr.length; i++){
if (Arr[i].length >= curLongest.length){
curLongest = Arr[i];
}
}
return curLongest.length;
}

findLongestWord("The quick brown fox jumped over the lazy dog");
``````
1 Like

function findLongestWord(str) {
var newstr = str.split(" ");
var sortedarr = newstr.sort(function(a, b) {
return b.length - a.length;

});

return sortedarr[0].length;
}

findLongestWord(“The quick brown fox jumped over the lazy dog”);

Hello, i wrote this code it permits me to put the longest word if i want it
function findLongestWord(str) {
var arr = str.split(" ");
var taille = arr[0];
for(var i = 1;i < arr.length;i++){
if(taille.length > arr[i].length){
taille = taille;
}else{
taille = arr[i];
}
}
return taille.length;
}

Here is other version that i wrote

``````function findLongestWord(str) {
var arr = str.split(" ");
var taille = arr[0];
for(var i = 1;i < arr.length;i++){
if(taille.length < arr[i].length){
taille = arr[i];
}
}
return taille.length;
}

``````

And this

``````function findLongestWord(str) {
var arr= [];
var chaine = str.split(' ');
for(var i = 0; i < chaine.length;i++){
arr.push(chaine[i]);
}
var sortie = arr[0];
for(var i = 1; i < arr.length;i++){
if(sortie.length < arr[i].length){
sortie = arr[i];
}
}
return sortie.length;
}
``````

my solution

``````function findLongestWord(str) {
return str.split(' ').map(function (x){return x.length;}).reduce(function(p,c){return p>c ? p :c;});
}

findLongestWord("The quick brown fox jumped over the lazy dog");``````
2 Likes

function findLongestWord(str) {
var longest,i,j;
var myArray=str.split(’ ');
var loop = myArray.length;
for(i = 0; i <= loop ; i++){
for(j = 0; j <= loop ; j++){
if(myArray[i].length < myArray[j].length){

``````  }
}
``````

}

}

findLongestWord(“The quick brown fox jumped over the lazy dog”);

This is the solution I used to pass:

Did you try “read, search” before asking? The hint to answer your mistake is in the error you were given: “str.splice() is not a function”…

There exists an `array.splice(`) method. Try instead to use `str.slice()`

1 Like

//using built in functions only

function findLongestWord(str) {
var x=str.split(" ");
var y= x.map(function(v){
return v.length;
});

y.sort(function(a,b){
return b-a;
});
return y[0];
}

findLongestWord(“The quick brown fox jumped over the lazy dog”);

My solution is exactly the same

This is how I handled it campers:

function findLongestWord(str) {

var sentence = str.split(’ ');

var longestWord = 0;

for(var i = 0;i < sentence.length;i++) {

``````if(longestWord < sentence[i].length) {

longestWord = sentence[i].length;

}
``````

}

return longestWord;
}

findLongestWord(“The quick brown fox jumped over the lazy dog”);

Although you did `str.split(" ")`，the variable `str` was still a string

You should did `str = str.split(" ")`, then `str` will be a array

``````function findLongestWord(str) {
var splitStr = str.split(" ");
word = " ";
for (var i in splitStr){
if (splitStr[i].length >= word.length){
word = splitStr[i];
}
}
return word.length;
}

findLongestWord("The quick brown fox jumped over the lazy dog");
``````

It’s my solution. It’s kind of similar but no if you look thoroughly

``````function findLongestWord(str) {
transform = str.split(' ').sort(function(x, y){
return y.length - x.length; // DESC -> y.length - x.length
});

return transform[0].length;
}``````
1 Like

I’m not understanding why this code does not work as a solution. the console keeps telling me “cannot read property length of undefined”. Help would be greatly appreciated!

function findLongestWord(str) {
var wordArray = [];
var lengthArray = [];

wordArray = str.split(’ ');

for (var i = 0; i <= wordArray.length; i++) {
lengthArray.push(wordArray[i].length);
}
lengthArray.sort(function(a,b) {
return a - b;
});

return lengthArray[lengthArray.length - 1];
}

findLongestWord(“The quick brown fox jumped over the lazy dog”);

It did the same thing to me as well. In this line:

change “<=” to just “<”. That should fix it and the loop should still function correctly.

Here is what I came up with.

function findLongestWord(str) {
var arr1 = str.split(" ");
var arr2 = [];
for (a = 0; a < arr1.length; a++) {
var c = arr1[a].length;
}
var arr3 = arr2.sort(function(a, b){return b-a});
console.log(arr3);
return arr3[0];
}

findLongestWord(“The quick brown fox jumped over the lazy dog”);

Here is my solution using reduce:

``````function findLongestWord(str) {
if (!str || !str.trim()) {
return undefined;
}

return str.split(/\s+/).reduce((prev, curr) =>
prev.length > curr.length ? prev : curr
).length;
}

findLongestWord("The quick brown fox jumped over the lazy dog");
``````

It worked! Thank you. I would have never guessed my inequality operator was the issue.

1 Like

Hello, I managed to make a solution without using .split() or .reduce() or any operation that modifies the entire string. Instead, the logic works by going through the string and checking whether it is a word character or not. If it’s not, it considers it a new word and changes maxCount. Here’s my code:

``````function findLongestWord(str) {
var curCount = 0;
var maxCount = 0;
var i = 0;
while (i < str.length) {
if (str[i].match(/\w/)) {//If it's a word character, it adds 1 to curCount
curCount++;
if (i === str.length - 1) { //If it's a word and we're at the end as well:
if (maxCount < curCount) {//If the current length is the longest
maxCount = curCount;//maxCount is changed
return maxCount;//Finally, return maxCount
} else {
return maxCount; //If maxCount is still the biggest, just return it.
}
} else {
i++; //If we're not at the end, we just increment i after incrementing curCount
}
} else { //If it isn't a word character (e.g. if it was a whitespace):
if (i === str.length - 1) {//If we're at the end:
if (maxCount < curCount) {
maxCount = curCount;
return maxCount;
} else {
return maxCount;
}
} else {//If we're not at the end:
if (maxCount < curCount) {//Compare maxCount and curCount
maxCount = curCount;
curCount = 0;
i++;//Increment, since we're not at the end
} else {
curCount = 0;//Reset
i++;//Increment
}
}
}//End of final else

}
return maxCount;//Just in case
``````

}

findLongestWord(“What if we try a super-long word such as otorhinolaryngology”);

I know it’s quite long, and there’s a whole mess of if statements… I wanted to ask whether this was any more efficient than the other solutions, or if it’s the same or potentially worse. Not sure how to check that.

4 Likes

My code -
Feedback appreciated. The only problem I had was the callback function in the sort method. Otherwise, it went smoothly. Is this the most efficient way?

function findLongestWord(str) {
var arrLength = [];
arr = str.split(" ");
for (var i = 0 ; i < arr.length; i++) {
arrLength.push(arr[i].length);
var sorted = arrLength.sort(function (a,b){
return a-b;
});

}
return arrLength[arrLength.length - 1] ;
}

findLongestWord(“The quick brown fox jumped over the lazy dog”);