# String Looping explainer

#32

Don’t use the yellow string use the green “letter” which is the variable it’s asking for.

#33

@Sukhvinder_Singh_Jho — thanks for the suggestion!!

#34

I completely agree, I am stuck on a later puzzle simply because this part if the course skips a logical step. In the for loop there is no reason to expect the variable letter to store each letter in grasshopper. I guess I expected an array such as [g,r,a,s,s,h,o,p,p,e,r] to store each element separated by a comma as letter in the loop. It would have helped if in the explanation it said “in the for loop each character in grasshopper is treated separately and stored one after the other as the variable letter until there are no more letters left to store and the loop ends”.

#35

Just remove the inner loop and print the value of variable…

#36

It’s so basic. I guess I complicated my attempt

#37

I consider for … of loop really confusing after I used classical loop (var x = 0; x < something; x++) successfully many times. It took me a while to realize what’s going on.

#38

Absolutely uninformative hints. How I must guess about operator “element”? By random picking of operators in “your solutions”?

#39

So, each letter is it’s own thing? How do I do it if I wanted every word in a sentence appear on a different line?

#40

Hey @Hashim_Warren,

Great question!

The reason each letter goes onto it’s own line is from the `print()` function (you can find out more about how that function works here).

To print out a word on each line you’d want to loop through every item in an array, rather than every letter in a string. For example, you could have code something like:

``````var myArray = ['first', 'glasses', 'hello']
for (var element of myArray) {
print(element)
}
``````

This would output:
first
glasses
hello

I hope this helps explain things!
H

#41

As for a person who nevwr even looked at coding or JS , the lesson was great , then seemed o jump with out basic instruction or explanation, i had to read all the post to get a bettwr understanding of the algorythm. Mabey a bit more explaining would help, from a new peraons point of view.

#42

What am I doing wrong?

#43

Your `print()` statement is using the string `'i'` instead of the identifier `i`. A string has quote marks around it and will be orange colored in the app. A string is just seen as plain text when it’s in your code – even though there’s a variable called `i`. The identifier `i` will be grey and have no quotes around it. Whenever you use the identifier in your code, it is interpreted as its value. In your code, `i` starts with the number value 0 and then goes up by 1 until it reaches 10 and then the loop finishes.

If you tap once on the `'i'` string inside of your print, you can delete it using the ⌫ button on the top row. make sure the quote marks are gone too. Then you can tap on the grey `i` button to insert the identifier into your `print()`.

–Frankie

#44

Its good you are teaching javascript with good and easy way. Thanks to you

#45

How it’s printing one letter at a time.
I’m confused .

#46
``````for (var x of 'grasshopper') {
print(x)
}
``````

A for loop has 2 parts:

1. The setup in parentheses `()`
2. The body in the curly brackets `{}`

The `print(x)` will print the value of the variable `x`. Note that this is different from `print('x')` which will always print the letter ‘x’.

Inside the setup, there is a left and right side: the variable left of the `of` and the value on the right side of the `of`. The item on the right side is what gets “looped through”. Often it is an array which means each item in the array gets it’s turn through the loop. In this case, we have a string instead of an array. When there is a string on the right side of the of, you can think of it as an array of single characters.

`for (var x of 'grasshopper')`

is like

`for (var x of ['g', 'r', 'a', 's', 's', 'h', 'o', 'p', 'p', 'e', 'r'])`

–Frankie

#47

Have not been on here in months. Found it interesting I could solve the problem several ways and still pass. Was that intended to be done? Really like that I can look up what others think is going on here. One thing that seems to be missing for me is ,how can I apply this and test it for real world applications? IE : does this loop help me write code faster? Will this help me in HTML write headers faster? Just like any experience, what can I take from this, rather am I just being a monkey see monkey do. Thanks: really like this application.

#48

I have no idea how the variable letter gets the value of grasshopper. Aren’t variables supposed to be assigned values beforehand? How was it done? Does the “of” piece of word assigns the variable letter a value automatically? I don’t get it. The lessons become more and more abstract without much of a logical explanation.

#50

When a variable is declared inside the setup of a For Of Loop, it will will be assigned to the 1st value of the thing after the `of`. After completing the body of the loop with that 1st value, it is assigned to the 2nd value and then runs the body of the loop again. This repeats until all the values have been assigned. The assignment is done for you just by using `for (...of...)`. You could write separate code with all of the assignments, but it would make it very long.

Continuing from my post above:

``````for (var letter of 'grasshopper') {
print(letter);
}
``````

is the same as doing:

``````for (var x of ['g', 'r', 'a', 's', 's', 'h', 'o', 'p', 'p', 'e', 'r']) {
print(letter);
}
``````

which is a quicker way of doing:

``````var letter = 'g';
print(letter);
letter = 'r';
print(letter);
letter = 'a';
print(letter);
letter = 's';
print(letter);
letter = 's';
print(letter);
letter = 'h';
print(letter);
letter = 'o';
print(letter);
letter = 'p';
print(letter);
letter = 'p';
print(letter);
letter = 'e';
print(letter);
letter = 'r';
print(letter);
``````

–Frankie

#51

I completed the script and the lesson keeps telling me it’s wrong can I get some help?

#52

Hey there, can you post a screenshot of the code that isn’t working?

–Ben