Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Writing Functions | Functions
Ninja Game
course content

Course Content

Ninja Game

Ninja Game

1. Basic Controls
2. Functions
3. Loops
4. If-Else Statements
5. Challenges

book
Writing Functions

In this chapter, we will explore the concept of functions in JavaScript and how they can be utilized in the Ninja Game. Functions are a fundamental part of programming that allow you to group a set of instructions under a single name. This makes your code more organized and reusable.

What are Functions?

A function is a block of code designed to perform a particular task. You can think of it as a mini-program within your program. Functions help you break down complex problems into smaller, manageable pieces. Once a function is defined, you can execute it whenever needed by calling its name.

To define a function in JavaScript, you use the function keyword, followed by a name, parentheses (), and a set of curly braces {}. Inside the curly braces, you write the code that you want the function to execute.

Functions can also accept parameters, which are values you pass into the function to customize its behavior. Parameters are specified inside the parentheses in the function definition.

Example

Let's look at an example from the Ninja Game to see how functions can be used to control the ninja's actions:

js

ninja.js

html

index.html

js

preset.js

copy

In this example, we have two functions: moveNinja and lootBox. The moveNinja function calls lootBox twice and moves the ninja to the right four times. The lootBox function moves the ninja up, picks a sushi, moves down twice, picks another sushi, and then moves back up.

Task
test

Swipe to show code editor

Goal: Collect all sushi.

Your task is to collect all the sushi on the map. To do this, you need to complete the lootPit(ninja) function and use it in your code.

Solution

Starter Map

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 1
js

ninja.js

html

index.html

js

preset.js

toggle bottom row

book
Writing Functions

In this chapter, we will explore the concept of functions in JavaScript and how they can be utilized in the Ninja Game. Functions are a fundamental part of programming that allow you to group a set of instructions under a single name. This makes your code more organized and reusable.

What are Functions?

A function is a block of code designed to perform a particular task. You can think of it as a mini-program within your program. Functions help you break down complex problems into smaller, manageable pieces. Once a function is defined, you can execute it whenever needed by calling its name.

To define a function in JavaScript, you use the function keyword, followed by a name, parentheses (), and a set of curly braces {}. Inside the curly braces, you write the code that you want the function to execute.

Functions can also accept parameters, which are values you pass into the function to customize its behavior. Parameters are specified inside the parentheses in the function definition.

Example

Let's look at an example from the Ninja Game to see how functions can be used to control the ninja's actions:

js

ninja.js

html

index.html

js

preset.js

copy

In this example, we have two functions: moveNinja and lootBox. The moveNinja function calls lootBox twice and moves the ninja to the right four times. The lootBox function moves the ninja up, picks a sushi, moves down twice, picks another sushi, and then moves back up.

Task
test

Swipe to show code editor

Goal: Collect all sushi.

Your task is to collect all the sushi on the map. To do this, you need to complete the lootPit(ninja) function and use it in your code.

Solution

Starter Map

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 1
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt