Question
Karel is so excited for New Years Day! Karel wants to build a firework. Write a program that has Karel create a firework of tennis balls.
The key in this program is to break the problem down into several smaller problems and use functions to solve each smaller problem.
The key in this program is to break the problem down into several smaller problems and use functions to solve each smaller problem.
Answers
// Karel will create a firework of tennis balls
// Define a function to move Karel to the starting position
function moveToStartPosition() {
move();
turnLeft();
move();
turnRight();
}
// Define a function to place a tennis ball
function placeTennisBall() {
putBeeper();
move();
}
// Define a function to turn Karel to the right
function turnRight() {
turnLeft();
turnLeft();
turnLeft();
}
// Define a function to create a firework of tennis balls
function createFirework() {
moveToStartPosition();
for (var i = 0; i < 4; i++) {
placeTennisBall();
turnRight();
}
}
// Call the createFirework function to create the firework
createFirework();
// Define a function to move Karel to the starting position
function moveToStartPosition() {
move();
turnLeft();
move();
turnRight();
}
// Define a function to place a tennis ball
function placeTennisBall() {
putBeeper();
move();
}
// Define a function to turn Karel to the right
function turnRight() {
turnLeft();
turnLeft();
turnLeft();
}
// Define a function to create a firework of tennis balls
function createFirework() {
moveToStartPosition();
for (var i = 0; i < 4; i++) {
placeTennisBall();
turnRight();
}
}
// Call the createFirework function to create the firework
createFirework();