« Back to Main Page

Code Challenge: Turtle Square

From Bold Idea Knowledgebase

View code challenge

Solving the Turtle Square challenge demonstrates students understand how variables are used in a program. The program asks the user to enter a size. The number entered by the user is stored in a variable called size . The student must use that variable in place of the numbers used in the forward() command.

Requirements

  1. Run the program and enter "50" for the size. The turtle should draw a 50x50 square.
  2. Run the program again and enter "100" for the size. The turtle should draw a 100x100 square.

Solution

Use the size variable in each of the "forward" commands. For example, turtle.forward(size) .

Example Solution

const turtle = new Turtle();

let size = Number(prompt("Enter a size between 10 and 100"));

turtle.pendown();

turtle.forward(size);
turtle.left(90);
turtle.forward(size);
turtle.left(90);
turtle.forward(size);
turtle.left(60);
turtle.forward(size);
turtle.left(98);