3.5.5 Hexagon Codehs
A regular hexagon has six equal sides and six equal angles. To draw a hexagon using a "turtle" (a cursor that moves around the screen), we need to determine the the turtle must turn at each corner.
If we want to draw an (8 sides), we
This exercise is more than just drawing a shape; it is a rite of passage that tests a student's understanding of variables, loops, and geometric logic. If you have found yourself stuck on this specific problem, or if you are an educator looking for the best way to explain it, this article provides a deep dive into the theory, the code, and the logic behind the solution. Before diving into the code, it is essential to understand why CodeHS assigns this task. This exercise typically appears in the Unit 3: Control Structures section of the Introduction to Computer Science in JavaScript course. 3.5.5 hexagon codehs
move(50); turnRight(60); move(50); turnRight(60); move(50); turnRight(60); // ... repeated 6 times While this works, it violates the (Don't Repeat Yourself). If you wanted to change the size of the hexagon, you would have to edit the code in six different places. This is bad coding practice. The Correct Approach: Using a For Loop The elegant solution uses a loop to handle the repetition. We know we need to perform the "move and turn" action exactly six times. A regular hexagon has six equal sides and six equal angles
In the world of introductory computer science, the transition from simple command execution to algorithmic thinking is a pivotal moment. For students navigating the CodeHS platform, specifically within the "Control Structures" module, Exercise 3.5.5: Hexagon represents exactly that moment. If you have found yourself stuck on this