44.5k views
1 vote
CodeHS Karel The Dog - 3.4.4: Go Down the Slide: Part Two

*CODE ONLY FOR THAT PART*
function start(){
// Go go the top of the stairs
for(var i = 0; i < 4; i + 1){
climbStep();
}


// Go down the slide
for(var i = 0; i <= 4; i++){
getABall();
}

}

// Makes Karel climb up one step
// Precondition: Karel is facing East at the bottom of a step
// Postcondition: Karel is facing East on top of the next step
function climbStep(){
turnLeft();
move();
turnRight();
move();
}

// Has Karel go down part of the slide to collect
// one ball.
// Precondition: Karel is facing East above a ball
// Postcondition: Karel is facing East on the next ball (if there is one)
function getABall(){
move();
turnRight();
move();
takeBall();
turnLeft();
}
PLEASE TRY TO FIX THE CODE

User Overburn
by
8.3k points

1 Answer

5 votes

Answer:

function start(){

// Go to the top of the stairs

for(var i = 0; i < 4; i = i + 1){

climbStep();

}

// Go down the slide

for(var i = 0; i <= 4; i = i + 1){

getABall();

}

}

// Rest of the code remains the same

Step-by-step explanation:

there are some issues with the incrementation of the loop variable 'i' in the first for loop. The correct way to increment 'i' by 1 is to use 'i = i + 1' or the shorthand 'i++'.

User Tibor
by
7.4k points