Code Challenge: Stick House Neighborhood
From Bold Idea Knowledgebase
Solving the "Stick House Neighborhood" challenge demonstrates a students' understanding of defining functions , calling functions , and parameters .
Requirements
-
The program should have a function called
drawHouse()
. Running the script after defining the functions should draw four houses as shown in the challenge. -
The function must accept two parameters,
baseColor
androofColor
. - 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
-
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) -
Next, define the parameters:
drawHouse(baseColor, roofColor)
. -
Add a line in the function before drawing the base:
turtle.color(baseColor)
-
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();