# String Cartography explainer

Aim of the puzzle: Write a function that takes a string and returns an object that has each character of the string as a property, with each property containing an array of indices where the character appears.
Walk through of solution:
The function you write in this puzzle will take a string, and return an object that shows what characters are in the string and all of the indices where those characters appear. For example, let’s call the function on the string `'bookkeeper'`:

``````mapString('bookkeeper');
``````

This will return an object that looks like this:

``````{
b: [0],
o: [1, 2],
k: [3, 4],
e: [5, 6, 8],
p: [7],
r: [9]
}
``````

The object has a separate property for each character in the string. Inside each property is an array containing the index positions where the characters appear.

Start writing the function by creating an empty object inside the function. For example:

``````let map = {};
``````

Then, add a classic for loop to iterate through the string.

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

}
``````

Inside the for loop, create another local variable to store the current character. For example:

``````let letter = string[i];
``````

Then, use an if else statement (a ternary can also be used) to check if a property already exists in `map` for the current letter. If the property does not exist, it will be undefined, which is falsy. The if else statement will look something like this:

``````if (map[letter]) {

} else {

}
``````

If the property already exists (meaning the current character has been seen before), then push the index of the current character to the array contained in the property. If it does not exist, create the array (with the index inside as the 1st item). It should look something like this:

``````if (map[letter]) {
map[letter].push(i);
} else {
map[letter] = [i];
}
``````

Finally, at the end of the function, return `map`.
Javascript Concepts: Functions, Objects, Properties, Strings, For Loops, If Statements, Truthiness

What am I missing

Hey there, I see 2 issues.

On the 1st line inside the function, `map` isn’t being given a value, which is why you are getting the red error message.

Set map to be an empty object: `let map = {}`.

The other issue I see is inside the else block. Instead of

``````map[letter] = i;
``````

Try:

``````map[letter] = [i];
``````

This is because we want `map[letter]` to contain an array containing all the indices where each letter appears. This else block is where we are creating that array, and we’re starting it off with an index.

Does that make sense? It might also help to take another look at the lesson that introduces this puzzle. It will walk you through step by step.

Hope this helps!
Ben

I can’t put map to be empty and I can’t put I in a bracket

Hello still need help

Hey there,

To add an object to the code, tap the object key `{}`. Then leave it empty. In the app, it will look like this:

``````let map = {
___: ___
}
``````

To create an array, tap the orange array key `[]`, then add `i` as an element. In the app, it might look like this:

``````map[letter] = [
i,
_
]
``````

As this course is quite challenging, it might help if you take a break from the Intro to Interviewing course and revisit some of the puzzles from Fundamentals I and II.

Hope this helps!
Ben

I thought I had fixed all the errors but it keeps saying it can’t find a map variable with it being where it is, and if I put it in the loop it obviously does nothing but gets rid of the error.

I just rebuilt it from the ground up and no longer getting the error but the code isn’t working correctly either

Hey there,

Hmm, I’m not seeing anything wrong with the code in your screenshot. Unless I’m missing something, that solution should complete the puzzle.

Can you submit a bug report in the app? That way I can take a look at the app’s logs and get a better picture of what’s going on under the hood.

To submit a bug report, you can either tap on the main menu button (in the top-left corner), then tap on Settings, then Report a Bug. If possible, try getting a screenshot of the feedback/error message you see when you run the code. That can be useful for debugging.

If you’re in a puzzle, you can tap on the top-right corner menu button, then tap Send Feedback.

Thanks,
Ben

After rewriting it exactly as I had it ended up working, so I don’t know if I can still submit a bug report or not. After the first rewrite I was trying to use a regular if statement instead of if else and it wasn’t working, not exactly sure why because as far as I understand they should be functionally similar if not identical, but the if else statement worked like a charm.

Glad it’s working! If you experience anything similar in the future, feel free to submit a bug report.

Thanks,
Ben

I’m having the same issue. The puzzle won’t let me advance and idk why.

I am having problems with understanding the logic inside the if statement.
I understand what it does but not completely sure how. It feels like a piece is missing. King of want to put
If ( map [letter] ==true)
Is there is a way to write the same statement with more code that will make the logic a bit more clear ?

Thank you very much

Hey there,

Neither the strict `===` nor abstract `==` equality operators would work here. We also don’t use the abstract equality operator in the app, as its usage is generally frowned upon (its weird behavior often leads to bugs).

Let’s say the current `letter` is `'a'`, and the value stored at `map.a` is an array that looks like `[1, 3]`.

That means that

``````if (map[letter] === true) {

}
``````

is the same as

``````if ([1, 3] === true)
``````

which is false, because one is an array, and the other is a boolean.

This is why we use truthiness here.

Now, what you could do to make the logic more clear is break off the code that checks if the property exists into its own function, and give it a name that is very clear. Like this:

``````function propertyExists(map, letter) {
if (map[letter]) {
return true;
else {
return false;
}
``````

Then you could call the function in your if statement, like this:

``````if (propertyExists(map, letter)) {
map[letter].push(i);
} else {
map[letter] = [i];
}
``````

Hope that clears things up. Let me know if you have any questions.
Ben

1 Like

Got the same error.

Hey there, it’s difficult to tell from that screenshot if the error message is being shown correctly or incorrectly, as I can’t see your code. Try posting another one and I can take a look.

Thanks,
Ben

Sry, my mistake.
I think these are better

Hey there, I think I see the issue. You’ve got 2 variables named `map`. One is an object, and is before the for loop, and the other is an array inside the for loop.

Try deleting the `map` that is inside the for loop. That should work.

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

It worked. Thanks, for the really quick answer.

Have a nice day