String Looping explainer


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


@Sukhvinder_Singh_Jho — thanks for the suggestion!!


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”.


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


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


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.


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


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?


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) {

This would output:

I hope this helps explain things!
H :slight_smile:


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.


What am I doing wrong?


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().



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


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

for (var x of 'grasshopper') {

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'])



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.


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.


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') {

is the same as doing:

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

which is a quicker way of doing:

var letter = 'g';
letter = 'r';
letter = 'a';
letter = 's';
letter = 's';
letter = 'h';
letter = 'o';
letter = 'p';
letter = 'p';
letter = 'e';
letter = 'r';



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


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