Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Nested Loops | Loops
JavaScript Ninja
course content

Kursusindhold

JavaScript Ninja

JavaScript Ninja

1. Basic Controls
2. Advanced Controls
3. Functions
4. Loops
5. If-Else Statements
6. Challenges
7. Array

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

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.

Opgave

Swipe to start coding

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 4
js

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

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.

Opgave

Swipe to start coding

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 4
Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Vi beklager, at noget gik galt. Hvad skete der?
some-alt