Зміст курсу
Ninja Game
Ninja Game
While Loops
Welcome to the chapter on while loops! In this chapter, we will explore how to use while loops to make our ninja more efficient in collecting sushi. We will also learn about some new methods that help the ninja understand its surroundings, as well as how to compare strings in JavaScript.
Understanding While Loops
A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The code inside the loop will continue to execute as long as the condition remains true. This is particularly useful when you want to perform an action multiple times without knowing in advance how many times it needs to be done.
Syntax of a While Loop
The loop will keep running the code block inside it until the condition evaluates to false.
let i = 0 while (i < 5) { console.log("Incrementing:", i); i++; }
Ninja's Surroundings
To help the ninja navigate its world, there are several methods available. The objectUp()
method allows the ninja to check what is in the cell directly above it. Similarly, objectDown()
checks the cell below, objectRight()
checks the cell to the right, and objectLeft()
checks the cell to the left. Each of these methods returns a string that describes the object in the adjacent cell, which can be either "wall"
, "sushi"
, or "empty"
.
String Comparisons
In JavaScript, you can compare strings using the ===
and !==
operators. The ===
operator checks if two strings are exactly the same, while the !==
operator checks if two strings are not the same. These operators are useful when you want to make decisions based on the ninja's surroundings.
Example
Let's look at an example that demonstrates how to use a while loop along with the ninja's methods to collect sushi from a column:
ninja.js
index.html
preset.js
In this example, the collectColumn
function uses a while loop to collect all the sushi in a column. The ninja checks if there is sushi above it and continues to pick and move up until there is no more sushi. After collecting, the ninja moves back down to its original position.
Swipe to show code editor
Goal: Collect all sushi.
Use the while loop to repeatedly check the ninja's surroundings and collect all the sushi on the map. Utilize the objectUp()
, objectDown()
, objectRight()
, and objectLeft()
methods to guide the ninja's movements and make decisions based on what is around it.
Рішення
Starter Map
Дякуємо за ваш відгук!
ninja.js
index.html
preset.js
While Loops
Welcome to the chapter on while loops! In this chapter, we will explore how to use while loops to make our ninja more efficient in collecting sushi. We will also learn about some new methods that help the ninja understand its surroundings, as well as how to compare strings in JavaScript.
Understanding While Loops
A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The code inside the loop will continue to execute as long as the condition remains true. This is particularly useful when you want to perform an action multiple times without knowing in advance how many times it needs to be done.
Syntax of a While Loop
The loop will keep running the code block inside it until the condition evaluates to false.
let i = 0 while (i < 5) { console.log("Incrementing:", i); i++; }
Ninja's Surroundings
To help the ninja navigate its world, there are several methods available. The objectUp()
method allows the ninja to check what is in the cell directly above it. Similarly, objectDown()
checks the cell below, objectRight()
checks the cell to the right, and objectLeft()
checks the cell to the left. Each of these methods returns a string that describes the object in the adjacent cell, which can be either "wall"
, "sushi"
, or "empty"
.
String Comparisons
In JavaScript, you can compare strings using the ===
and !==
operators. The ===
operator checks if two strings are exactly the same, while the !==
operator checks if two strings are not the same. These operators are useful when you want to make decisions based on the ninja's surroundings.
Example
Let's look at an example that demonstrates how to use a while loop along with the ninja's methods to collect sushi from a column:
ninja.js
index.html
preset.js
In this example, the collectColumn
function uses a while loop to collect all the sushi in a column. The ninja checks if there is sushi above it and continues to pick and move up until there is no more sushi. After collecting, the ninja moves back down to its original position.
Swipe to show code editor
Goal: Collect all sushi.
Use the while loop to repeatedly check the ninja's surroundings and collect all the sushi on the map. Utilize the objectUp()
, objectDown()
, objectRight()
, and objectLeft()
methods to guide the ninja's movements and make decisions based on what is around it.
Рішення
Starter Map
Дякуємо за ваш відгук!