String Cartography explainer

Hi Grasshoppers,

I’ve got the problem, that in my code it seems to be an error, which I just don’t understand. After I completed each line, I had a look in here and found, that I had the same as a lot of people here. There ist still the error, that the code misses a variable for letter, which I obviously have, haven’t I?

Might you have a look?

Hey there, the issue is in the setup for your for loop:

for (let i = 0; i < letter.length; i++)

When this line of code runs, letter has not yet been declared, so the computer doesn’t know what it is. Change letter to string.

string is the parameter for the function. When the function is called, this could be a value like 'animal' or 'balloon'.

We want to loop through this string, which is why we use the parameter in the for loop like this:

for (let i = 0; i < string.length; i++)

Inside the for loop, letter is declared to give us a quicker and cleaner way to refer to the current letter of the string as the string is being looped through, like this:

let letter = string[i];

On the 1st loop, letter will be string[0], for example, the 1st 'a' in 'animals', or the 'b' in 'balloon'.

On the 2nd loop, letter will be string[1], and so on…

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

Hi Ben,

I see. I totally have overseen it. Thanks so much!

1 Like

I have some trouble using “let”, something is missing and I could not find what…

Hey there, I see the issue.

The code block in your for loop contains the letter variable declaration, but then the code block is closed with }, like this:

for (let i = 0; i < string.length; i++) {
    let letter = string[i];
}

The if...else statement is outside this code block, and therefore can’t see the letter variable.

Move the if…else statement so that it is inside the for loop, like this:

for (let i = 0; i < string.length; i++) {
    let letter = string[i];
    if (map[letter]) {
        map[letter].push(i);
    } else {
        map[letter] = [i];
    }
}

Then, outside the for loop:

return map;

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

Thank u, the code works now:)

For some reason it’s not letting me use brackets it replaces the whole word

Hey there,

Sometimes the behavior of adding code depends on what code is highlighted when you tap the key. There are also 2 different bracket keys. The orange is used to create an array, and the pink one is used to access an item in an array.

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

My code gets an error that map is not defined even though I declare it before the for loop. What am I missing?

Hmm, that looks like the right code to me…

Is map being used outside the function at all? The variable is scoped (meaning it lives inside) the mapString() function. Trying to use it outside the function will cause this error.

Hope this helps!
Ben

Thanks so much for the super quick response. I totally missed a console.log call of the map outside of my function that was part of the original, preexisting code for this activity. I deleted that line and everything works now.

Funny that we didn’t have the option to use the var button (instead of just let) for this activity, which would have prevented my problem in the first place.

Thanks again for the help.

Hi, I’ve looked over everything and can’t seem to find what’s incorrect!

Great news! I figured it out!
-my else statement was “map.letter = [i]”
-the correct statement is “map[letter] = [i]”
hopefully this helps someone out!

1 Like

Please I don’t fully grasps the logic going on here. Could you please explain to me bit by bit like a baby. Thanks

Hi, are there any other exercises or places where I can practice similar puzzles like this one. I’m starting to feel like another user who said they were just following the instructions and copying, as opossed to really learning. I understand the logic and what the code is supposed to do, but I couldn’t write it without copying from the explanation in the previous puzzle. Tnx

It’s behaving correctly in that what I want it to do, it is doing. But it won’t let me complete the lesson!

Hey there, your function takes a parameter called string, but in your for loop you are using word. Try changing i < word.length to i < string.length. That should do the trick.

Ben

Please what’s wrong with this? It’s not passing me through.

Hey there, add i inside the parentheses of .push(), like this:

map[letter].push(i)