Letter by Letter explainer

Aim of the puzzle: Use indexing with a string to access specific characters in the string.
Walk through of solution: Just like arrays, strings also have indexes, with each index representing a single character in a string (including spaces and symbols).

For example:

let string = 'bang!';

The 'b' is at string[0], the 'a' is at string[1], etc…

Also like arrays, strings have a .length property, that will return the number of characters (again, including spaces and symbols) in the string.

The string 'bang!' has a length of 5, as there are 5 characters total.

To complete this puzzle, add a parameter name to the function, for example: string.

Add a classic for loop to iterate through the input string, and inside the code block, use console.log() to print each character to the console.

Finally, call the function with a string to print each character to the console.

Sample code solution:

function printCharacters(string) {
    for ( let i = 0; i < string.length; i++) {
        console.log(string[i]);
    }
}

printCharacters('strings');


Javascript Concepts: Classic For Loops, Strings, Indexing, .length, console.log()

3 Likes

This is not working can someone please tell me how to avoid this

Hey there, I like your username. Happy to help!

In this puzzle you’ll write a line of code that adds a property to an object, after that object has been declared.

You’ve done this before with dot notation, like this:

let animal = {
    name: 'Aleksandr Poochkin',
    species: 'Dog'
};
animal.age = 5;

In this puzzle, you’ll do the same thing with bracket notation, like this:

let animal = {
    name: 'Aleksandr Poochkin',
    species: 'Dog'
};
animal['age'] = 5;

If you’re experiencing issues where the assignment operator = is appearing in the wrong place, make sure that all of animal['age'] is highlighted before you tap the = key.

Hope this helps!
Ben

1 Like

Hi guys, I can’t figure this one out ! Any help is appreciated!

Hey there,

It looks like you have the console.log(string[i]) in the setup of the for loop, instead of in the code block.

Try deleting the console.log(), and replacing it with i++. Then put the console.log() inside the code block. Like this:

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

Hope this helps!
Ben

Thank you Ben ! It works . I was stuck for a while !!
Jalal

1 Like

Hey there,
I have an issue with this exercise as well.
Can you explain please what I did wrong?

Hey there,

string is the parameter for the function, and can be used inside the function. You’re using it both inside and outside the function. It’s the last line of code that’s causing the red error message.

On the last line, delete string. Replace it with an actual string using the str key, then type in some text. Like this:

printCharacters('example text');

Hope this helps!
Ben

Hey Ben,

Thank you very much! It helped!

Have a great day!
Olga

Hi. i know this sounds weird but i can’t get anything between the parenthesis on printCharacters.


and im stuck on this one and id like to get going asap :cry:

1 Like

hey ben is maddison need at you a quack

Hey there,

At the top of the code, on the line:

function printCharacters(___) {

add a parameter with a name like string (you can name it whatever you want). Click twice on the underline to bring up the keyboard.

Once you’ve created and named the parameter, a new button will appear on the code keyboard. For example, if you named the parameter taco, you’ll see a button named taco on the keyboard.

Next, change your for loop to use this parameter. Right now you have it looping through a string with the text 'string'. Change it to loop through the parameter, like this:

for (let i = 0; i < taco.length; i++) {
    console.log(taco[i]);
}

At this point, your function declaration is complete.

Now, when you call the function at the bottom, you’ll see an underline inside the parentheses. Press the str button to add a string there, and type in some text.

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

thank you very much! it worked.

have a great day!

1 Like

Hey Ben! I’m baffled and confused, so much so I believe I encountered an error, please help!

Hey there, your function looks fine to me. I think the issue is that you’re calling the function inside a console.log(). This isn’t necessary for this function, as there’s a console.log() inside the for loop that will print the text we want to see.

Hope this helps!
Ben

I’m having the same issue as others here. The printCharacters funcrion won’t allow me to create the string.

Hey there, double tap on that underline to bring up the keyboard. Naming a parameter is just like naming a variable in Grasshopper. You’ll have to use your device’s keyboard and manually type in the name, rather than using a key on the code keyboard.

Hope this helps!
Ben


When I enter the parameter in the function, it creates another placeholder for a second parameter. Any ideas on that?

You can ignore it. That just means you have the capability to add another parameter, but it doesn’t mean you should or shouldn’t. There’s a similar behavior when you create an array or object, you’ll always see an extra underline where you can add another property or item.

When you press the run code button, the app will filter out these underlines before running your code.