The Longest Word explainer

Aim of the puzzle: Write a function that takes an array of strings and returns the longest string in the array.
Walk through of solution: At the end of this course, you will be writing a function that takes a string and an array of strings, and returns the longest string in the array that is a subsequence of the other string.

For now, write a function that simply returns the longest string in an array of strings. This will be a useful helper function to use later.

To start, create a local variable to store the longest string. Set its value to be an empty string. You will be adding to it later in the function. For example:

let longestString = '';

Then, add a for loop to iterate through the array of strings. Inside the for loop, add an if statement that checks if the current string being looked at is longer than the longest string. It should look something like this:

    for (let word of array) {
        if (word.length > longestWord.length) {
            longestWord = word

If a word is found that is longer than longestWord, then that word is assigned to longestWord.

At the end of the function, return the longest string.
Javascript Concepts: Functions, Strings, .length, For Loops, If Statements, Arrays

I need help I don’t get it

Hey there,

In your function, you are creating a variable called longestWord, and storing the string 'word' in it.

Take another read through the Puzzle Explainer post at the start of this thread. It breaks the puzzle down step by step. If there is anything unclear about the explanation or you have any questions about it, please let me know and I’ll be happy to answer them!


Hope this helps!

There is let of I don’t know where you find that and I don’t know how to make a loop

Hey there, no worries.

In the app, when you tap the for...of key, the for loop will automatically put:

for (var element of ___)

It’s completely fine to use var, and I’ll be updating the solution code for this puzzle to use var, as it isn’t currently possible to put let in a for...of loop in the app.

It looks like you’re almost done with the puzzle! You just need to return the longest word from the function.

If you need a refresher on for loops, return statements, or any other JavaScript concepts, I suggest taking another look at the Fundamentals I and II courses again and then coming back to the Intro to Interviewing course.

Good luck, and let me know if you have any more questions!

1 Like

I got this to work, I think, but it tells me to ask for grasshopper support.

Hey there,

It looks like you have 2 different if statements that are both updating the value of BigWord.

The first if statement you have is checking if the length of the subsequences array is greater than the length of the BigWord string. If true, it is setting the value of BigWord to be the subsequences array.

The second if statement in your code is checking if the length of the current string in the array is greater than the length of the BigWord string. If true, then it is updating BigWord to be this string.

This 2nd if statement is what you want. Deleting the first if statement should solve the puzzle.

Hope this helps!

Thank you, it did help

1 Like

What does this actually mean?

Where am I going wrong?
I am iterating through the array, adding the word to longWord, the testing it and returning the longWord (which should be the longest.)

I posted this over a week ago… will I get a reply soon?

Hey there, thanks for your patience!

I see what the issue is. Remove the line longWord = array[i].

Before your for loop, longWord is being set to an empty string, but inside the for loop, it’s being set again to be the current element in the array. Then the test is checking if the current array element is longer than longWord.

Because longWord has the value array[i], the if statement is checking if array[i].length > array[i].length.

Take out the line longWord = array[i], and the puzzle should complete.

Hope this helps!