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).