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
Additional Code (hidden code that runs before the puzzle’s code):

let indices = [3,7];
let minIndex = [4];
2 Likes

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.

3 Likes

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

2 Likes

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.

7 Likes

My code looks just like your code but it does not work at the current time also I get no hint just asks if I need help check forums. I also get the same result as the example on the page. I sent a feedback with screenshot of my code

Hey there, try posting the screenshot here, and I’ll take a look and determine if it’s a bug or if there’s something to change in the code.

Thanks!
Ben

Solved It was my error I misnamed the indices as array.

It was for (index of array)
Instead of for (index of indices)
I forgot to rename the array

![Screenshot_2019-11-25-20-17-39|281x500](upload://m9PONDEFEjaRqKzpjK4gbuKEr4Z.png
worked but not successful@_@
Any explanation?

Hey there, try editing your post so the screenshot is uploaded properly. Then I can take a look and give you some pointers.

Thanks,
Ben

can someone explain me what minIndex is? i dont understand it.

edit: if i understand it right: minIndex is the place (in a word) where to begin to search for a subsequence(?). But why can’t you begin to search at the beginning of a word?

You’re correct! minIndex is the minimum index in a string where we want to search for a letter/character.

Let’s say we want to check if the string 'to' is a subsequence of the string 'otto'.

Let’s map 'otto' to an object as an example:

{
  o: [0, 3],
  t: [1, 2]
}

This shows that there’s an 'o' at index 0 and 3, and a 't' at 1 and 2.

We search for the 't' in 'to' and see that there’s one at index 1, so now we want to look for the 'o'.

We look for the o, and see that there’s one at index 0, which is the first 'o' in 'otto'. However, index 0 is before index 1, so we know we should skip it and look for the one at index 3.

minIndex is how we know to skip this. This variable tells us the smallest possible index a letter can be at if the string is to be a subsequence.

Does that make sense? It’s not the easiest concept to explain, so let me know if you have any questions.

Ben

6 Likes

Still. It doesn’t allow me to keep on, this is my answer, I think it is right


Thanks
Solved! Sorry I changed indices for array and now it’s Ok.

2 Likes

I understand that I have put “return false” in the wrong location. I would like to know in this case, why does the function output only “false”? To my understanding, I should receive “false” (3<4), and then “8” (7>4) after looping through the array “indices”.

Hey there, try moving return false down 1 line so it’s not inside the for loop.

Because it’s inside the for loop, it’s returning false before it is finished looping through all of the elements. That’s causing the issue.

Hope this helps!
Ben

Thank you for your response, though it does not answer my question.

With the given code, I would expect the 2 outputs:
First, “false” because 3 is not larger than 4.
Second, “8” because 7 is larger than 4. Therefore 7+1=8

Because the loop runs through all elements I would expect such result with this code.


Help me

Hey there, once a function hits a return statement, it stops running and returns a value.

In your function, the return false was inside the for loop.

This means that the for loop looked at the 3, and asked “Is this 3 greater than or equal to 4? No. I should return false.” The for loop never looked at the 8 because the function stopped running.

Hope that makes sense. Let me know if you have questions.
Ben

1 Like

Hey there, change for (var element of indices) to for (var element of array).

Hope this helps!
Ben