In the Right Place explainer

Aim of the puzzle: Create a helper function that will check if a character is in the right order.
Walk through of solution: In the previous puzzle, you wrote a function that checked if all of the characters in a string existed as properties in an object map of another string. It did not check the order, however.

In this puzzle you will write a helper function that can be used by puzzle’s function to determine if a string is a subsequence.

As a reminder, the function in that puzzle is comparing a string against an object. For a string like 'balloon' object will look something like this:

{
  b: [0],
  a: [1],
  l: [2, 3],
  o: [4, 5],
  n: [6]
}

Each character in 'balloon' has been turned into a property, with the indices where the character appears stored as an array.

The function in this puzzle will take 2 parameters, an index representing the minimum possible index a character would need in order to be in order, and an array.

Start the puzzle by adding a for loop to the function to iterate through the array. For example:

  for (let index of array) {

  }

Inside, add an if statement that checks if the current index is greater than or equal to the minimum possible index:

  for (let index of array) {
    if (index >= minIndex) {
      
    }
  }

If this is true, we’ll need to return that index + 1. Adding 1 will tell function calling this helper function to update the minimum possible index for the next character it searches for.

  for (let index of array) {
    if (index >= minIndex) {
      return index + 1;
    }
  }

At the end of the function, return false. This line of code will only run if no numbers are found in the array that are greater than or equal to the minimum possible index. Returning false will tell the function calling this helper function that the word it is checking is not a subsequence, because the letter are out of order.

Javascript Concepts: Functions, Strings, Indexing, Arrays, For Loops