Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
If-Else Statements | If-Else Statements
Ninja Game
course content

Course Content

Ninja Game

Ninja Game

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

book
If-Else Statements

In this chapter, we will explore the concept of if-else statements in JavaScript. If-else statements allow us to make decisions in our code based on certain conditions. This is a fundamental concept in programming that enables us to control the flow of our programs.

Understanding If-else Statements

An if-else statement evaluates a condition and executes a block of code if the condition is true. If the condition is false, it can execute an alternative block of code. This is how we can make our programs respond differently to different situations.

Here's the basic structure of an if-else statement:

123456
const condition = true if (condition) { console.log("Condition is true") } else { console.log("Condition is false") }
copy

The "!" Symbol

The "!" symbol is used to negate a boolean expression. If a condition is true, using "!" will make it false, and vice versa. This can be very useful when you want to execute code only when a condition is not met.

For example:

1234
const condition = false if (!condition) { console.log("Condition is false") }
copy

Example

Let's look at an example that uses if-else statements to help our ninja collect all the sushi on the map. The ninja will move around the grid, picking up sushi while avoiding walls.

js

ninja.js

html

index.html

js

preset.js

copy

In this example, the ninja will continue to move and pick up sushi until it encounters walls both above and to the right. The if-else statement checks if there is a wall to the right. If there is, the ninja moves up and picks up sushi. Otherwise, it moves to the right and picks up sushi.

Task
test

Swipe to show code editor

Goal: Collect all sushi.

Use if-else statements to navigate the ninja through the grid and collect all the sushi. You can use all the features you've learned so far, including loops and conditionals, to complete this task.

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 4. Chapter 1
js

ninja.js

html

index.html

js

preset.js

toggle bottom row

book
If-Else Statements

In this chapter, we will explore the concept of if-else statements in JavaScript. If-else statements allow us to make decisions in our code based on certain conditions. This is a fundamental concept in programming that enables us to control the flow of our programs.

Understanding If-else Statements

An if-else statement evaluates a condition and executes a block of code if the condition is true. If the condition is false, it can execute an alternative block of code. This is how we can make our programs respond differently to different situations.

Here's the basic structure of an if-else statement:

123456
const condition = true if (condition) { console.log("Condition is true") } else { console.log("Condition is false") }
copy

The "!" Symbol

The "!" symbol is used to negate a boolean expression. If a condition is true, using "!" will make it false, and vice versa. This can be very useful when you want to execute code only when a condition is not met.

For example:

1234
const condition = false if (!condition) { console.log("Condition is false") }
copy

Example

Let's look at an example that uses if-else statements to help our ninja collect all the sushi on the map. The ninja will move around the grid, picking up sushi while avoiding walls.

js

ninja.js

html

index.html

js

preset.js

copy

In this example, the ninja will continue to move and pick up sushi until it encounters walls both above and to the right. The if-else statement checks if there is a wall to the right. If there is, the ninja moves up and picks up sushi. Otherwise, it moves to the right and picks up sushi.

Task
test

Swipe to show code editor

Goal: Collect all sushi.

Use if-else statements to navigate the ninja through the grid and collect all the sushi. You can use all the features you've learned so far, including loops and conditionals, to complete this task.

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 4. 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