I cant understand why this code doesnt work HELP!

function findLongestWordLength(str) {
  let arrSplit = str.split(" ");
  let arsl = arrSplit.length;
  let index1 = 0;
  let index2 = 1;

function recurse() {
  if (arsl == 1) {
    return arrSplit[index1];
  }
  if (arsl > 1) {
    if (index1.length > index2.length) {
      arrSplit.splice(index2);
    }
    if (index1.length < index2.length) {
      arrSplit.splice(index1);
    }
    if (arsl > 1) {
      recurse()
    }
  }
  
  }
  return arrSplit[index1];
}

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

function findLongestWordLength(str) {
let arrSplit = str.split(" ");
let arsl = arrSplit.length;
let index1 = 0;
let index2 = 1;

function recurse() {
if (arsl == 1) {
  return arrSplit[index1];
}
if (arsl > 1) {
  if (index1.length > index2.length) {
    arrSplit.splice(index2);
  }
  if (index1.length < index2.length) {
    arrSplit.splice(index1);
  }
  if (arsl > 1) {
    recurse()
  }
}

}
return arrSplit[index1];
}

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 13597.66.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.109 Safari/537.36.

Challenge: Find the Longest Word in a String

Link to the challenge:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

You’ve defined a function recurse() inside of findLongestWordLength() but then you never use it. That’s OK because you don’t need it :slight_smile:

Don’t even think about this in JS terms yet. If you are reading the following sentence:

“The quick brown fox jumped over the lazy dog”

How would you go about figuring out the longest word in that sentence. Literally tell us in plain English how you would figure it out.

Once you can explain that then I think it will be much clearer how to solve this problem in JS.

2 Likes

Hi @lucas.h.cahill !

Also remember that you are supposed to return a number.
If you are returning arrSplit[index1] wouldn’t that just be the word ‘The’?

I would reset the problem and follow @bbsmooth 's advice because there is a much simpler approach that will cause a lot less headache for you :grinning:

OK awesome thank you so much! I wasn’t aware of this feature. I will be sure to use Preformatted text in the future!

I didn’t even realize that I never actually called the recurse function ! Lol. Thank you for your response!

I was thinking that if I compared the length of every word, storing the word with the largest length that would make the most sense, however I couldn’t really wrap my head around how I would actually go about doing that.

So eliminating the indices in an array created from the string seemed logical to me.

The function should return the length of the longest word in the sentence, so you don’t need to store the word itself, just the length. You have the right idea though, go through every word in the sentence and keep track of the longest word length you find. You are on the right track by splitting the string into an array. I bet you know how to go through each item in an array, right? You just have to keep track of the longest word you find in the array and return it. I wonder if JS provides a mechanism for storing a number (the length of a word) and updating that number if you find an even longer word?