Course Content
Ninja Game
Ninja Game
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:
const condition = true if (condition) { console.log("Condition is true") } else { console.log("Condition is false") }
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:
const condition = false if (!condition) { console.log("Condition is false") }
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.
ninja.js
index.html
preset.js
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.
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
Thanks for your feedback!
ninja.js
index.html
preset.js
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:
const condition = true if (condition) { console.log("Condition is true") } else { console.log("Condition is false") }
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:
const condition = false if (!condition) { console.log("Condition is false") }
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.
ninja.js
index.html
preset.js
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.
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
Thanks for your feedback!