Loops of Color explainer

The aim of this puzzle: To draw three boxes using a loop and an array of colors.

Walkthrough of the solution: The code view starts with a for loop that loops through an array of the colors pink, purple and blue. For each item in the array a pink box is drawn, since the code inside the loop’s block {} starts as drawBox('pink').

To draw a pink, purple, then blue box update drawBox('pink') to reference the looping variable element, like: drawBox(element).

Sample code solution:
(Tap below to reveal)

for (var element of ['pink', 'purple', 'blue']) {
    drawBox(element)
}

JavaScript Concepts: Calling Functions, For Loop, Variable Declarations, Data Structures (arrays)
Grasshopper Concepts: drawBox()

1 Like

What ia the different between this practice with ‘follow the recepie practice’ ? Why in the ‘follow the recepie practice’ the result there are new Line when print the ingredient…
Sorry my English not good

“Loops of Color” and “Follow the Recipe” are both alternate puzzles for the Fundamentals I puzzle “String Looping”.

In most of the courses in Grasshopper, each puzzle is actually part of a group of 3 puzzles. All 3 of the puzzles in a group are similar to each other.

If you’re having trouble solving a puzzle, you can tap “Reveal Solution”, which will show you the answer code and then take you to one of the alternate puzzles in the group.

Once you complete any puzzle in the group, the 2 alternate puzzles will appear in the Practice course.

Hope this answers your question!
Ben

1 Like

I’ve been having a lot of trouble with all of these questions. Whenever I change the answer to what it suggests it says that it must be inserted as a var. When I try to insert it as a var it says that you can’t do that.

For example I put as the solution drawbox(element) which was incorrect as it needed to be var. When I tried to do that it said it couldn’t be done with a red warning message

Hey there, try posting a screenshot of the code that isn’t working, and I’ll take a look.

As it shows in the screenshot I’m trying to change the drawbox element to a variable but it won’t let me. I’m sure I’m just misunderstanding what I’m supposed to be doing for the task but I just can’t get my head around it.

Hey there, try deleting the string and then tapping the element key on the code keyboard to add it into drawBox().

Hope this helps!

Thank you! That’s sorted it

I’m just not clear on HOW this works?

Hello @DJ_Nelson, how this works is the code uses an array to find out what to draw. In this example, the array “element” has three colors in it; pink, purple, and blue. The computer draws a pink box, and then a purple box, and then a blue box because that’s the order in which it was presented.
Hope this helps! :hugs: