Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Nested 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
Nested Loops

In this chapter, we will explore the concept of nested loops. Nested loops are loops within loops, and they are a powerful tool in programming that allows us to perform repeated actions in a structured way. By using nested loops, we can efficiently navigate and manipulate complex structures, such as grids, which is exactly what our Ninja will be doing in this chapter.

Imagine our Ninja moving through a grid, collecting sushi. The grid is made up of rows and columns, and our goal is to collect all the sushi scattered across this grid. To achieve this, we can use nested loops: one loop to move across the rows and another loop to move through each column.

Let's take a look at an example to understand how nested loops work in the context of our Ninja Game:

js

ninja.js

html

index.html

js

preset.js

copy

In the example above, we have two functions: ninjaController and lootColumn. The ninjaController function uses a while loop to move the Ninja across the grid from left to right. For each column, it calls the lootColumn function, which contains another while loop to move the Ninja upwards, collecting sushi as it goes. Once the Ninja reaches the top of the column, it uses a for loop to move back down to its original position.

By nesting the while loop inside the lootColumn function within the while loop of the ninjaController function, we can efficiently collect all the sushi in each column before moving to the next one.

Tarea
test

Swipe to show code editor

Goal: Collect all sushi on the map.

Use nested loops to navigate through the grid and collect all the sushi.

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

ninja.js

html

index.html

js

preset.js

toggle bottom row

book
Nested Loops

In this chapter, we will explore the concept of nested loops. Nested loops are loops within loops, and they are a powerful tool in programming that allows us to perform repeated actions in a structured way. By using nested loops, we can efficiently navigate and manipulate complex structures, such as grids, which is exactly what our Ninja will be doing in this chapter.

Imagine our Ninja moving through a grid, collecting sushi. The grid is made up of rows and columns, and our goal is to collect all the sushi scattered across this grid. To achieve this, we can use nested loops: one loop to move across the rows and another loop to move through each column.

Let's take a look at an example to understand how nested loops work in the context of our Ninja Game:

js

ninja.js

html

index.html

js

preset.js

copy

In the example above, we have two functions: ninjaController and lootColumn. The ninjaController function uses a while loop to move the Ninja across the grid from left to right. For each column, it calls the lootColumn function, which contains another while loop to move the Ninja upwards, collecting sushi as it goes. Once the Ninja reaches the top of the column, it uses a for loop to move back down to its original position.

By nesting the while loop inside the lootColumn function within the while loop of the ninjaController function, we can efficiently collect all the sushi in each column before moving to the next one.

Tarea
test

Swipe to show code editor

Goal: Collect all sushi on the map.

Use nested loops to navigate through the grid and collect all the sushi.

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