Putting It All Together explainer

The aim of this puzzle: Find the string’s longest subsequence from an array.

Walkthrough of the solution: The general outline for solving this puzzle is:

  1. Create a string map
  2. Create a new array to store valid subsequences
  3. Loop through all the strings in the array (dictionary)
  4. Check if each string is a valid subsequence
  5. If it is, add it to the new array you made
  6. Finally, use the longestWord() function to return the longest valid subsequence from your new array

Creating the map is simple because you already created a function for it: mapString(). Store the result in a new variable; you could name it map.

Creating a new array to store any matches is similar: let listOfSubsequences = []. This can stay empty, because we’ll fill it up 1 at a time inside the loop.

A for of loop will work nicely to check each of the strings in the array: for (let word of dictionary) {...}

The isSubsequence() function from the previous puzzle is useful now. It needs 2 arguments: a string and a map object, then it will return whether the string is a subsequence of that map object (or rather, the string that the map represents). Since the function returns true or false, it makes sense to use this as a test in an If Statement.

If the test is true, then the word is a subsequence, and it should be added to the listOfSubsequences array. This can be done using the .push() method.

As the For Loop continues, it will add all the valid subsequences to the listOfSubsequences. Once we have that list, we only need to return the longest one. Luckily, we made a function for that: longestWord(). After the loop, add return longestWord(listOfSubsequences).

Would it be possible for you to walk me through step by step.

I put the code in as you have said, but it keeps saying I need to come here. When I got to the return part, it kept telling me to do what I was doing. Could you possibly share the answer code, because I have no clue what to do to fix mine?

i think there is a similar bug going on here as the one in the puzzle “in the right place”. once i get to the part where you put on the last line of function declaration “return longestword(listofsubsequences)” it stops giving hints and just directs you to the forums. here is a screenshot of my finished answer. i could swear it’s right but it wont finish the puzzle. what about my solution is wrong? or is it a glitch like i think it is?

Hey there, it looks like the issue is the element = inside the if statement. The = is the assignment operator. What this does is assign whatever value is on the right of the = to the variable on the left. This means that as isSubsequence returns true and false, element is now true or false, and not the word in the dictionary.

That means that listOfSubsequences is then just an array that looks like [true, true, true, true].

Try taking it out, so the if statement is just:

if (isSubsequence(element, map)) {

We’ll work on adding a new feedback message for this code run, so other users with the same issue can get a better idea of what the problem is.

The rest of your code looks fine to me, so that should solve the puzzle. Let me know if you have any questions.

Hey there @Sebastian_Donall and @Bholi85, thanks for your patience in waiting for a reply over the long holiday weekend.

As this is a “graduation” puzzle for the course, we don’t post the full answer code in the Puzzle Explainer post, but I’m happy to walk you both through the solution step by step.

There are different ways to arrive at the solution (one of the joys of programming), but I’ll guide you through one that I think makes sense.

In this puzzle, you are going to take all of the helper functions you created in the previous few puzzles, and put them all together to create the longestMatch() function.

This function will take a string (let’s call it s) and an array of strings (let’s call it dictionary), and returns the longest string in dictionary that is a subsequence of s.

Let’s break it down to figure out what we need to do.

First, we need to map s so we know what letters are in it and where the letters appear. We can use the mapString() function to do this, and save what the function returns in a new variable. It will look something like this:

let map = mapString(s);

Next, we should create an empty array. This array will eventually store all of the strings in dictionary that are subsequences of s, but to start, we will leave it empty. It should look something like this:

let listOfSubsequences = []

Next, we need to figure out which strings in dictionary are subsequences, so let’s create a for loop to iterate through it:

for (var element of dictionary) {

Then, inside the for loop, we can use the isSubsequence() helper function in an if statement to check each string one at a time and determine if the string is a subsequence of s.

This function takes the current string from the array (in this case, element), and the mapped string (the map variable we created at the start of the function). The function will return true or false.

The if statement will look something like this:

for (var element of dictionary) {
  if (isSubsequence(element, map) {


Next: If the function returns true, then we want to add the current string (element) to the array we created earlier in the function (the one we left empty). We want this array to contain all of the strings in dictionary that are subsequences.

We can do this with the .push() array method, like this:

for (var element of dictionary) {
  if (isSubsequence(element, map)) {

Finally: When this for loop is finished looping, listOfSubsequences will contain all of the strings that are subsequences. However, we just want to return the longest string. We can use the longestWord() helper function.

It will look something like this:

return longestWord(listOfSubsequences);

Or it could look like this:

let longestSubsequence = longestWord(listOfSubsequences);
return longestSubsequence;

This puzzle is one of the more difficult ones in the Grasshopper curriculum, so if you have any trouble with my explanation or you want me to clarify anything, don’t hesitate to ask additional questions.

Hope this is helpful!


Thank you so much Ben. Definitely a challenging puzzle. I plan to spend much time reviewing this and going through intro to interviewing several more times. Appreciate it!


Thanks for the help. I actually had that at first, which leads me to think there was a glitch or something. It worked now, so thank you.

1 Like

Thank you thank you thank you. you guys at grasshopper are amazing. i cant believe i didn’t catch that. thank you for your swift help. it worked perfectly. grasshopper is one of the best if not “THE Best” and least greedy learn-to-code apps out there; and it has great support.

1 Like

I finally got it right! After days of trying. Happy dance.

1 Like

Not sure where I’m going wrong… I think I’m doing everything Ben talks about (with the exceptions of different variable names) and now I’ve stopped getting feedback and just get “error: e.apply is not a function.” Anyone know what to do with that?Screenshot_20190811-150219_Grasshopper

1 Like

Hey there, that sounds like a bug.

Try submitting a bug report through the app by tapping on the top-left menu button and navigating to settings, then “Report a Bug”.

Thanks for your patience,

I tried to write ALL code from begin to end! Holly-Molly! All functions. And I was very amazed hadn’t been finding some code-buttons.

1 Like

Hello! I’m pretty sure my code is correct, why is it not working? :slight_smile:

Hey there, you’re super close!

This line:

let map = mapString(stringSequence)

Should be:

let map = mapString(string)

This is because string is the parameter for the function. stringSequence is a string imported into the puzzle to be used in the function call.

Hope this helps! Let me know if you have any questions.

What did I do wrong?

It works but says it wrong

Hey there, you’re so close!

Your return statement is inside the for loop. This means that the first time the for loop runs, the return statement runs, and the function stops executing.

Move the return statement down a line, so it’s below the closing bracket } of the for loop, and above the closing bracket of the function.

Hope this helps!

I am having a problem regarding this. I require your help

Hey there, try posting a screenshot of your code and I’ll take a look.