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 (var element of array) {

  }

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

  for (var element of array) {
    if (element >= 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 (var element of array) {
    if (element >= minIndex) {
      return element + 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

this explanation does not help me. it raises more questions than it answers. this solution is not even possible to be inputed by the user of your app into the puzzle. the app only lets us use forof loops not classic for loops. i even followed the apps instructions and even though i have inputed exactly what it says to, it acts like i didnt do it at all and just keeps repeating the same hint. on the forums the solution they give is not what the app wants. please help. i feel like the puzzle"in the right place" might be glitched or buggy. i thought i was understanding everything up to this point but now im so confused.

I agree!
I have no clue why I have not deleted this app.
I guess I’m just too dumb.

i havent deleted the app yet because i really want to learn and i’m waiting on grasshopper support to answer my question. i give it mabe a week and then im deleting it. why would they provide you with a solution that uses a method that the puzzle in the app wont allow the user to employ.

Hey there, thanks for flagging this issue, and I appreciate your patience in waiting for a response over the long holiday weekend.

It looks like there’s a problem with the logic that decides which feedback message appears, which is causing the same feedback message (the one about the if statement) to appear even if the code is correct. I’m taking a look at this now and you can expect to have this fixed in next week’s update.

In the meantime, the puzzle will still complete if the function is finished, so I can help walk you through it now.

First, the function needs to iterate through the array, so tap the for...of key to add a for of loop to the code. It will look like this:

function findNextIndex(array, minIndex) {
  for (var element of array) {

  }
}

We then want to add an if statement that checks if the current number being checked is greater than or equal to the minIndex (this is where the bugged feedback message is). Let’s add the if statement:

function findNextIndex(array, minIndex) {
  for (var element of array) {
    if (element >= minIndex) {

    }
  }
}

If this is true and a number is found that is greater than or equal to minIndex, we want to return that number + 1 (adding 1 will allow the function that calls findNextIndex to check the next character in the string it is looking at, otherwise it would check the same character over and over).

At the end of the function, we want to return false. This line will only run if no number is found that is greater than or equal to minIndex.

The completed function will look like this:

function findNextIndex(array, minIndex) {
  for (var element of array) {
    if (element >= minIndex) {
      return element + 1;
    }
  }
  return false;
}

Hope this helps! If you have any questions, or encounter any more issues, don’t hesitate to let me know.
Ben

thank you so much for the help. i thought mabe i was just not getting it, or i was crazy. i knew it had to be a glitch. i have used a lot of learn-to-code apps, and grasshopper is the best for javascript by far, in my humble opinion.

2 Likes