« Back to Main Page

Code Challenge: Stick House Neighborhood

From Bold Idea Knowledgebase

View code challenge

Solving the "Stick House Neighborhood" challenge demonstrates a students' understanding of defining functions , calling functions , and parameters .

Requirements

  1. The program should have a function called drawHouse() . Running the script after defining the functions should draw four houses as shown in the challenge.
  2. The function must accept two parameters, baseColor and roofColor .
  3. The four drawn houses should each have different colors for the roof and base, as shown in the challenge (colors do not have to match exactly)

Solution

  1. The first step is to define a function called drawHouse() , then move all of the "house drawing code" inside the function. (the house-drawing code has comments that say where the code begins and ends)
  2. Next, define the parameters: drawHouse(baseColor, roofColor) .
  3. Add a line in the function before drawing the base: turtle.color(baseColor)
  4. Add a line in the function before drawing the roof: turtle.color(roofColor)

Helpful Prompts

Here are some helpful prompts you can give the student to nudge them in the right direction.

  • It looks like it's not working. Look in the JavaScript console to see if there is an error message!
  • It says "drawHouse is not defined". What do you think we need to do?
  • Imagine if I told you to make a "foofoo sandwitch". You might say, "I don't know how to do that! You need to give me instructions.".
  • In the PB&J activity you created a function to tell a robot how to make a PB&J. What do you need to do to tell the computer how to draw a house?

Example Solution

const turtle = new Turtle();

function drawHouse(baseColor, roofColor) {
    // Draw a square for the base
    turtle.color(baseColor);
    turtle.pendown();
    turtle.right(90);
    turtle.forward(50);
    turtle.right(90);
    turtle.forward(50);
    turtle.right(90);
    turtle.forward(50);
    turtle.right(90);
    turtle.forward(50);

    // Draw a triangle roof
    turtle.color(roofColor);
    turtle.left(90);
    turtle.forward(10);
    turtle.right(135);
    turtle.forward(50);
    turtle.right(90);
    turtle.forward(50);
    turtle.right(135);
    turtle.forward(70);

    // Go to the starting point
    turtle.penup();
    turtle.backward(10);
    turtle.right(90);
    turtle.backward(50);
}

// Draw four different houses
drawHouse();
turtle.goto(100, 100);
drawHouse();
turtle.goto(200, 0);
drawHouse();
turtle.goto(100, -100);
drawHouse();