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

Contenido del Curso

Ninja Game

Ninja Game

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

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

12345
let i = 0 while (i < 5) { console.log("Incrementing:", i); i++; }
copy

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:

js

ninja.js

html

index.html

js

preset.js

copy

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.

Tarea
test

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.

Solución

Starter Map

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 3
js

ninja.js

html

index.html

js

preset.js

toggle bottom row

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

12345
let i = 0 while (i < 5) { console.log("Incrementing:", i); i++; }
copy

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:

js

ninja.js

html

index.html

js

preset.js

copy

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.

Tarea
test

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.

Solución

Starter Map

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 3
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt