Nesting Repeats (Teacher's Choice)
- GH Feedback 8/30/15: More of a question - Should the triangles exercise be something they turn in? Seems like it's somewhat involved, and a good example of abstraction.
It is also possible to nest one repeat
block inside another. Since snap processes scripts from top to bottom, the first repeat
will begin first, and the second repeat
will have to complete all its repetitions before the first repeat
continues.
-
Build a script that draws a red triangle, duplicate it twice, and make the two new scripts draw a smaller blue triangle and an even smaller green one.
- To duplicate a script, right-click (or control-click) on the first block of the script (in this case, the
repeat
block) and choose "duplicate".
-
Then, change the inputs as the video shows:
- Try each script by itself to be certain what it does. Then clear the stage.
-
Now, insert the entire script for the blue triangle between the
move
and turn
blocks of the red triangle, like this.
Predict what will happen when you run this script.
- Then, try this script to see what it does.
-
Using the scripts you have, find a way to make this picture.
- If you like, embed one more triangle, half the size of the last one, in the same way.
-
This animation shows another example of embedding one script inside a similar script. (You'll make predictions first, so don't build it yet.)
- Before building this script, sketch what each of the two individual scripts will draw before they are combined.
- Predict the number of blue shapes that the combined script will draw.
- Sketch the shape that the combined script will draw.
- Now, build the combined script and try it out.
- Experiment: Embed another square, half the size of the last, in the same way. How many of these smallest squares does the script draw? How could you have predicted that number before trying it?
- Predict then experiment: Next, embed one more square, half the size of the last, in exactly the same way. But first, try to predict the number of tiny squares it will draw before trying it.