String Looping explainer


#1

The aim of this puzzle: Print the word 'grasshopper' with each letter on a new line.
Walkthrough of the solution: You want your code to do something like: print('g'); print('r'); print('a'); and so on, but using a for loop. This type of for loop has three parts: an item that you are going to loop through, a temporary variable to store each item as it goes through the loop, and the code inside the loop that runs each time. In this case, the item you want to loop through is the letters of the word “grasshopper”, and the temporary variable we store each letter will be called letter. So the for loop will take the first letter in the string 'grasshopper', which is 'g', and store it in the variable letter. Inside of the loop, you want to print whatever that variable is storing. Once it prints, the temporary variable goes on to the next letter of the word and then prints again.
Sample code solution:
(Tap below to reveal)

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

JavaScript Concepts: Code Block (for loop), Loops, Variable Declaration, Calling Functions, Identifiers
Grasshopper Concepts: print()


#2

I need help explaining the Grasshopper string Loop? Can someone provide me with examples with code?


#4

I think a loop goes through each thing and runs the code for that thing. Like here each thing is a letter in “grasshopper”, and the code print(letter) runs for each letter and prints them out individually. The puzzle after this one goes through an Array of colors, and the code runs for each color.


#5

Isn’t it kind of strange to teach ES6 stuff in a beginner’s guide to JS? This might not be available when they actually start to program.


#6

Hey @jtvjan,

Great question! Our aim is to teach users real-world JavaScript skills, and that includes being up to speed on the latest changes. Our plan is to address language versions in a future course to help prepare users for the issue you identified.

Let me know if you have more feedback!
H :slight_smile:


#7

Hi and thanks for a great tool!

In my opinion, as this is the first example of a loop, one should not confuse the student by simutaniously introducing the concept of ‘grasshopper’ as an array of characters.

It would have been less confusing if it was a classic integer count from 0 to 10. It would be more pedagogic.

The enumrated string could be another example of looping together with an explaination of the string operation.

What do you think?


#8

Hey @Sukhvinder_Singh_Jho,

Thanks for the feedback!

We’ve played with the ordering of how to introduce loops for the past few months, and — as an intro to the idea of code being repeated — we found users were more successful starting with a simple for…of loop that iterates through something, rather than a classic for loop.

Good point on the array vs. string idea. We haven’t seen much confusion with that so far, maybe because the idea of breaking up strings into individual letters has already been covered by the drawBoxes() function. But I’ll keep an eye out for that going forward.

Thanks,
H :slight_smile:


#9

I have spend almost 2 hours on this, its terrible. I honestly trying my best to under how to use for loop correctly, however im able to run the program but i dont understand how to stop the loop, it keeps looping!. I hope someone else can help me with some inside on what im doing wrong here. Thanks!

This is what i have so far:

for ( var letter of ‘grasshopper’ ) {
print( ’ g ’ );
print( ’ r ’ );
print( ’ a ’ );
print( ’ s ’ );
print( ’ s ’ );
print( ’ h ’ );
print( ’ o ’ );
print( ’ p ’ );
print( ’ p ’ );
print( ’ e ’ );
print( ’ r ’ );
}


#10

@PaPa For each letter in the word ‘grasshopper’, it will run whatever is in { }. So in your code, it is spelling out grasshopper one line at a time 11 times since there are 11 letters in grasshopper.

Instead, it only wants one line printed per letter, so you only want one print( ) inside { }.

To get it to print the correct letter that corresponds with the letter in ‘grasshopper’, you would put the correct variable inside the ( ) rather than a string.

To view the solution, tap or click the blurred area of the first post.


#11

I got it to work but I don’t understand why?


#12

That’s a lot of looping, @Mike_grasshopper!

You don’t need two for loops to solve this, although you can solve it with the code you’ve shared.

Instead of putting a for loop inside the other for loop, you can simply update the print() function to reference the looping variable letter.

A for loop repeats the piece of code inside the curly brackets (also called the block). When looping through a word, the for loop’s block {} repeats for each letter of that word. The letter is stored in the looping variable. In this case, the looping variable is called letter. This means for the first loop of print(letter) ‘g’ gets printed, then on the second loop ‘r’ gets printed.

Hoping this helps explain things!
H :slight_smile:


#13

How can I solve this check error message in blue bar. I get it right but there is no congratulations or moving next challenge. Blue bar keeps popping up with that message


#14

Hey @brekker,

You’re super close to finishing, you just have some extra code. There are two for loops in your code, you only need the second one. Try deleting the first for loop then run you code again.

H :slight_smile:


#15

Thanks, it worked,Heather. God bless.


#16

There’s something in this puzzle that’s puzzling me. Is it default that once the block has been iterated through once, the results of the next iteration are displayed in the next line cuz even in the example code solution, the newline() function has never been called.


#17

Every time you print to the console ( print() in this case ), it will print on a new line.


#18

@Grasshopper_Heather How do I know I would be looping through each letter of the word ‘grasshopper’? Letter is not a function, correct? For example, What if ‘grasshopper’ was 0123456789 - would I be using var number?


#19

@Grasshopper_Heather so I went further and this is what I thought I would see in this example and didn’t VAR Letter (‘g’,’r’,’a’,’s’…). I would understand looping through the array, but not the way it was written.


#20

Example could use additional details on how variable letter is being assign each and every letter in grasshopper.


#21

If the variable letter is supposed to be a character array composed of the word grasshopper, I would have expected to see [ ] which is what the app taught us to be the symbol for arrays.