A loop repeats a block of code a specified number of times. Each loop cycle is a controlled step: begin, act, update, then continue or stop.
Syntax concept (informal, not language-specific):
“repeat these steps N times.”
Log in to post a comment.
// UNIT 3 – Loop Toy 2 (fixed)
// Title: Triangle Chain (No Extra Leg)
Canvas.setpenopacity(1);
const turtle = new Turtle();
// variables
let side = 60;
let turn = 120;
let repetitions = 3;
// starting position
turtle.penup();
turtle.goto(-90, -20);
turtle.pendown();
let c = 0;
function walk(i) {
if (c < repetitions) {
// --- draw one equilateral triangle ---
turtle.forward(side);
turtle.right(turn);
turtle.forward(side);
turtle.right(turn);
turtle.forward(side);
// at this point we are back at the starting corner of the triangle
// --- move to the start of the next triangle WITHOUT DRAWING ---
turtle.penup();
turtle.right(turn); // restore heading to the right (0°)
turtle.forward(side); // slide to the next start point
turtle.pendown();
c++;
return true;
}
return false;
}