Find the Longest Word in a String...How could I start it?

Hi,

Could somebody give me a hint how to start this and why to start it that way? I have checked the string.length and string.prototype.split() in javascript pages, but I don’t really understand.

I guess I have to split it into an array, and then check, which of the words/elements in the array is longest, but how…?

Which part don’t you understand?

This is the perfect way to start any programming challenge. Put it into words before you write a single line of code. You’re definitely on the right track. Imagine you had an array of numbers. How would you write a function that goes through each number and returns the largest?

3 Likes

Spoilers below. Try to figure it out on your own first.

There are several ways to do this. One way would be to set a variable largest to 0, then iterate through your array of numbers. If the current number is greater than largest, set largest to the current number.
Or google javascript find largest number array. This will point you to a way to do it with Math.max.

You are on the right way. First create an algorithm or couple sentences before doing coding part.

So use split() method for couple times or watch some videos. Why people use split(), why they shouldnt etc. After that check what is string.length property. You can use console.log() to exercise both. Than check explanation on FCC for that challenge. Remember if and for and while and all those concepts. After that you still dont undersand check FCC wiki on github. They are explaining solution.

Btw there is no one solution. Remember that.

1 Like

I am not sure how to split it into an array. I try to find out how I can use the examples from string.prototype.split in this case.

Hi,

A hint would be to looking at the last 10 challenges you have had in the Javascrypt object-oriented functional programming section of the course.

Cheers!

1 Like

You’re on the right track with what you said in your post, so where exactly are you stuck? Is it in writing the code itself or do you not know how to do a particular thing.

It’s a good idea to post some code and indicate what problem you’re having

Is kinda weird at first. Because you can’t understand what you don’t really use.

After some time, you will eventually understand them and you will rethink these questions in computer terms. At the moment. Think of how a human would do this.

You have a full length of string. Or a sentence and you are ask to point out the longest word in that sentence.

Analyze carefully the process you took and write that out in pseudo code. As in sentence by sentence instruction how to solve this problem.

Then you will research how to accomplish each task. For example.

Step 1: check each word and count how many alphabets there are.

Step 2: write them down and check which one is the longest.

Stuff like that.

Feels that it’s really hard for me to figure out the answer for this.

I just get this far and it’s still wrong:

function findLongestWord(str) {
  var splits = str.split(' ');
  console.log(splits);
  return str.length;
}
var str = "The quick brown fox jumped over the lazy dog";
findLongestWord("The quick brown fox jumped over the lazy dog");

You are returning the length of the original string here.
You have to iterate through your splits array.

I don’t get anywhere.

function splitString(stringToSplit, separator) {
  var arrayofstrings = stringToSplit.split(separator);
 
}
function findLongestWord(str) {
  return str.length;
}

var str = "The quick brown fox jumped over the lazy dog";
var space = ' ';
splitString(str, space);

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

Again, you’re returning the length of the string “The quick brown fox jumped over the lazy dog”. That can’t work.

I don’t really understand this task…What should I split? Should I split this “The quick brown fox jumped over the lazy dog” into an array of words?And then calculate which one is the longest? I have problems understanding the meaning of this task.

Yes, exactly! I think you got it now.

This still goes wrong.

var myString = "The quick brown fox jumped over the lazy dog";
var space = " ";

function splitString(stringToSplit, separator) {
  var arrayOfStrings = stringToSplit.split(separator);
}
splitString(myString, space);

function findLongestWord(str) {
  return str.length;
}

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

I tried also other ways to turn it into an array first.

I tried also this (found it on w3resource.com)

string_to_array = function (str) {
     return str.trim().split(" ");
};
console.log(string_to_array("The quick brown fox jumped over the lazy dog"));

function findLongestWord(str) {
  return str.length;
}

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

The problem is in findLongestWord. For the umpteenth time, the assignment is to find the longest word, not the length of the original string.
This might help to get you unstuck:

HTH

I just got through this and think I used a rather odd method. I’ll talk through it if the OP is still having trouble it might help.

** This is how I figured it out, as always there are multiple ways and usually a better one, but understanding is the most important part **

  • First part you already got, take the string and split it into an array. This way you can test each word instead of the whole thing.
    "This is a string" becomes an array containing [This, is, a, string]
  • Now to test the length of each of those elements, you can use a switch, if statements or loops. Loops seem the easiest and simplest for me.
  • Since this task asks only for the length of the longest word, the actual words aren’t really necessary. I decided to iterate through the array and count the length of each word adding it to a new array.
  • With a new numbered array, I sort it using the method taught in an earlier challenge which gives an ordered array
    [ 1, 2, 4, 5] for example, based on the array above
  • Finally I simply return the final element from this array, knowing it will be the largest number.

Whew, probably a lot more going on than necessary, but I did it myself and like the way I came up with a solution. Also, twenty minutes ago I was getting pretty annoyed by it, so just find that one thing holding you back and you’re good.

1 Like

CoderInFreeCodeCamp you have to split a sentence (one long stream of characters/letters, the sentence is a string) into words (separate strings). Afterwards you need to count each word’s letters iterating over an array (words, not the sentence). you can for example keep the result in the separate variable. There’s a big chance you’ll have to change the variable (result) if you find a longer word. At the end you have to provide the number which represents the number of characters in the longest string/word.
Don’t forget to let us know whether you’ve solved it or not.