Follow the Recipe explainer

Aim of the puzzle: Use a For…of loop to iterate through an array.
Walk through of solution: A For…of loop is one of several ways to iterate through an array in javascript. The For…of loop declares a looping variable, and then loops through each element in an array, with the looping variable referring to the current element at each iteration. For example:

for (var letter in englishAlphabet) {
  print(letter);
}

The above For…of loop declares the variable letter and iterates through the englishAlphabet array. Each time it iterates (loops), letter refers to the current element in the array. On the first iteration, letter has the value 'a'. On the 2nd loop, letter has the value 'b'.

In this puzzle, a For…of loop is used to iterate through the recipe array. The looping variable ingredient will refer to the current element in recipe as the for loop iterates through the array.

To complete the puzzle, delete the string 'list' from the for loop, and replace it with the identifier recipe.

Sample code solution:

var recipe = [
    'basil',
    'olive oil',
    'salt'
];
for (var ingredient of recipe) {
    print(ingredient);
}

Javascript Concepts: Calling Functions, For Loop, Variable Declarations, Data Structures (arrays)
Grasshopper Concepts: print()

Hi,
Could you please clarify why we don’t need to create a variable “ingredient”? How the system knows that “ingredient” is what is inside var = recipe?
In other words, what if instead of “ingredient” I wrote “blablabla”. Would the below work:

for (var blablabla of recipe) {
print(blablabla);
}

?
Thank you!

Hey there, you actually are declaring a variable inside the for loop, and you are totally correct that you can name it whatever you want!

Let’s take a look at the example you posted:

var recipe = ['basil', 'olive oil', 'salt']

for (var blablabla of recipe) {
   print(blablabla);
}

var blablabla is a variable declaration! This is the looping variable.
recipe is the array we want to loop through.

Together with the for and of keywords, this is telling the computer, "for every blablabla of the recipe array, print the blablabla"

The first time the loop iterates, blablabla has the value 'basil'. Then the line print(blablabla) runs and prints 'basil'.

On the next loop, blablabla has the value 'olive oil', and so when print(blablabla) runs it prints 'olive oil'.

Hope this helps! Let me know if you have any questions.
Ben