String Looping explainer


#22

@Ritwik_Neema,

Fantastic question! Awesome curiosity. print() is a custom function we’ve created, that moves the cursor to a new line at the end. This means if you call multiple print() functions they will all start on their own line.

Keep questioning things!
H :slight_smile:


#23

Hey @Y_ask_Y,

The name of the looping variable doesn’t matter. In this puzzle, since it’s the first intro to looping, we’ve named the variable something to indicate what it’s holding (i.e. a letter). But you can name the looping variable whatever you want, it’s just to allow you to reference it inside the loop’s block {}.

For example, you could have code like:

for (var x of 'grasshopper') {
print(x)
}

Which would print out each letter of grasshopper on a new line.

Hoping this helps!
H :slight_smile:


#24

Still not working for me.


#25

Hi @Joshua.D,
It looks like you have placed the string ‘letter’ inside of the print() statement instead of the variable letter. Which is why the app is printing out ‘letter’ for every letter in ‘grasshopper’ rather than actually printing out the letter to spell out ‘grasshopper’.

for (var letter of 'grasshoper')
This loops through grasshopper, with every run, a letter is assigned to letter in order of the string. So for the first run, letter === ‘g’, then letter === ‘r’, then letter === ‘a’, etc. All the way until the loop reaches r, prints ‘r’, then the loop ends.

Hope this helps! :grin:


#26

But the instructions on the top tell me otherwise. Also there is no “===” button option I already tried doing

print(‘g’);
print(‘r’)!
Ect

Non of the solutions presented by the forum or your comment seem to work or be possible.


#27

Tried this and all it does is keep looping grasshopper over and over again non stop.


#28

Yes ! It is not working !
Please Help !


#29

Hi again,

As I progressed the course I saw the general for loop and understood that it might be more complex.

The middle approach is to use a string array like this:

[‘One’,
‘Two’,
‘Three’,
.
.
.];

This might be the best of both worlds?

Thanks for the fantastic support :+1:t4:


#30

Hi @Joshua.D,
The letter === ‘g’ was just to describe how a loop works, not what you have to do inside the loop. You do not have to print each letter of grasshopper in the loop that will assign each letter of grasshopper to letter. With each letter being assigned to letter by the for…of loop, the inside of the loop only needs to print(letter); to print grasshopper vertically.


#31

Ok I was misunderstanding I got it to work by using the green “letter” variable instead of the string “letter”. Thanks. :+1:


#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 :slight_smile:


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