String Looping explainer


#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