What does the mystery function do?

function mystery() {
while (noBallsPresent()) {
move();
}
}
JavaScript

Karel moves until it is on a ball

Karel moves once if there is no ball present

Karel moves until it puts down a ball

Karel checks if there is no ball on the current spot and then moves once

1 answer

The mystery function in your provided JavaScript code is designed such that Karel continues to move while there are no balls present.

Here's a breakdown of the function:

  • while (noBallsPresent()): This line creates a loop that continues to run as long as the condition noBallsPresent() returns true (i.e., there are no balls present).
  • Inside the loop, the command move(); instructs Karel to move forward.

Based on this logic, the function effectively makes Karel move in its environment until it encounters a ball, at which point the loop will terminate because noBallsPresent() will no longer be true.

Given the options provided, the correct interpretation of what the mystery function does is:

Karel moves until it is on a ball.