javascriptrefactoringturtle-graphicslines-of-code

What program uses the fewest lines of code to draw a 3x3 grid using a turtle?


I'm challenging myself to find the fewest lines of code needed to draw a 3x3 grid using a turtle in JavaScript. This is based off the code.org lesson. My teacher's best was 57 loc, and I found a way to get it down to 50 loc. Is there any way to get it below 50 loc? The only things we can use are moveForward(), turnLeft(), penUp(), and penDown().

Grid: enter image description here


Solution

  • I think we can solve this with a third of the LOC you quoted:

    function grid(side) {
        pendown()
        repeat(2, function () {
            sign = 1;
            repeat(3, function() {
                forward(side * 3);
                left(sign * 90);
                forward(side);
                left(sign * 90);
                sign = 0 - sign;
            });
            forward(side * 3);
            left(90);
        });
    }
    

    A different JavaScript turtle package but basically the same idea:

    enter image description here

    You should post your own solution as part of your question.