Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Exploring Decomposition | Functions
JavaScript Ninja

bookExploring Decomposition

Decomposition is the idea of breaking a big problem into smaller, easier parts. Instead of writing one long block of code, we divide the work into multiple functions. Each function handles one clear task, making our code easier to read, reuse, and maintain.

In the Ninja Game, decomposition helps us build complex behavior from simple building blocks. By calling functions inside other functions, you create a chain of actions that the ninja performs step by step.

ninja.js

ninja.js

copy
  • ninjaController organizes the mission. Instead of writing all movements here, it simply calls lootTunnel three times;
  • lootTunnel contains the detailed steps for entering a tunnel, grabbing sushi, and exiting;
  • At the end of each tunnel, lootTunnel calls moveToNextTunnel, which prepares the ninja for the next run.

This layered approach makes the code much clearer. Each function has one job, and together they form a complete solution.

Task

Swipe to start coding

Solution

Explore other courses in Catalog

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 5
ninja.js

ninja.js

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

bookExploring Decomposition

Swipe to show menu

Decomposition is the idea of breaking a big problem into smaller, easier parts. Instead of writing one long block of code, we divide the work into multiple functions. Each function handles one clear task, making our code easier to read, reuse, and maintain.

In the Ninja Game, decomposition helps us build complex behavior from simple building blocks. By calling functions inside other functions, you create a chain of actions that the ninja performs step by step.

ninja.js

ninja.js

copy
  • ninjaController organizes the mission. Instead of writing all movements here, it simply calls lootTunnel three times;
  • lootTunnel contains the detailed steps for entering a tunnel, grabbing sushi, and exiting;
  • At the end of each tunnel, lootTunnel calls moveToNextTunnel, which prepares the ninja for the next run.

This layered approach makes the code much clearer. Each function has one job, and together they form a complete solution.

Task

Swipe to start coding

Solution

Explore other courses in Catalog

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 5
ninja.js

ninja.js

some-alt