Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Nested Loops | Loops
New Script JS
course content

Kurssisisältö

New Script JS

New Script JS

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.

Tehtävä

Swipe to start coding

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 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.

Tehtävä

Swipe to start coding

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 4
Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt