Please enable JavaScript to use CodeHS

Karel's Built in Commands

Commands

move();
 
turnLeft();
putBall();
 
takeBall();

Functions

Writing a Function

Writing a function is like teaching karel a new command word.

Naming Functions: You can name your functions whatever you want, but you can't have spaces in the function name.

Remember that each open bracket { must match with a close bracket }

function turnRight() {
	turnLeft();
	turnLeft();
	turnLeft();
}

function turnAround() {
	turnLeft();
	turnLeft();
}

function yourFunctionName() {
  // Code that will run when you make a call to
  // this function.
}

// The start function is a special function that runs
// automatically when the program runs. This function
// is only used in a couple of CodeHS courses
function start() {
    // Code that will run automatically
}

Calling a Function

In order for Karel to follow your new function command, you must call that function in your code. You call it just like the other built-in Karel commands -- functionName();

// Writing the function spin
function spin() {
    turnLeft();
    turnLeft();
    turnLeft();
    turnLeft();
}

// Calling the function spin
spin();

Conditional Statements

If Statements

Remember that each open bracket { must match with a close bracket }
if (condition) {
  // code that will run if the condition is true
}
if (condition) {
  // code that will run if the condition is true
} else {
  // code that will run if condition is not true
}

Example of if statements

if (frontIsClear()) {
    move();
}

if (ballsPresent()) {
	takeBall();
} else {
	move();
}

Karel Conditions

Don't forget the () at the end
frontIsClear()
leftIsClear()
rightIsClear()

facingNorth()
facingSouth()
facingEast()
facingWest()

ballsPresent()
			
 
frontIsBlocked()
leftIsBlocked()
rightIsBlocked()

notFacingNorth()
notFacingSouth()
notFacingEast()
notFacingWest()

noBallsPresent()
			

Loops

Remember that each open bracket { must match with a close bracket }

While Loops

while (CONDITION) {
  // Code that will run while the CONDITION is true.
  // Once the CONDITION is no longer true,
  // it will stop.
}

Example of while loops

/* This moves Karel to a wall */
while (frontIsClear()) {
	move();
}

For Loops

Note: you can use both "let" and "var" in your for loops (they are two types of JavaScript variables). Use whichever you have been taught in your course.
for (let i = 0; i < COUNT; i++) {
  // Code that will run 'COUNT' times
}

// Can also use "var" to define the "i" (depends on your course)
for (var i = 0; i < COUNT; i++) {
  // Code that will run 'COUNT' times
}

Example of for loops

/* This puts down 10 balls */
for (let i = 0; i < 10; i++) {
	putBall();
}

// Or, if your course teaches "var":
for (var i = 0; i < 10; i++) {
	putBall();
}
You can have multiple statements or function calls in a for loop.
/* This puts down five balls and moves after each one */
for (let i = 0; i < 5; i++) {
	putBall();
	move();
}

// Or, if your course teaches "var":
for (var i = 0; i < 5; i++) {
	putBall();
	move();
}
Use for-loops when you want to repeat something a fixed number of times.
Use while-loops when you want to repeat something as long as a condition is true.

Comments

Comments

/* A multi-line comment describes your code
 * to someone who is reading it. */

// Use single line comments to clarify code.