Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Align two columns | Challenges
JavaScript Ninja

book
Challenge: Align two columns

Welcome to the "Align Two Columns" challenge!

Let's recap the ninja methods and features you've learned:

Ninja Methods:

  • goRight(), goLeft(), goUp(), goDown(): Move the ninja one cell in the specified direction.

  • pickSushi(): Pick up sushi from the map and add it to your inventory.

  • putSushi(): Place sushi from your inventory onto a cell.

  • objectUp(), objectDown(), objectRight(), objectLeft(): Determine the object in the next cell in the specified direction, returning "wall", "sushi", or "empty".

Programming Concepts:

  • Functions: Create reusable blocks of code to perform specific tasks.

  • Decomposition: Break down complex problems into smaller, manageable parts.

  • Loops: Use for and while loops to repeat actions.

  • Conditional Statements: Use if-else statements to make decisions based on conditions.

Task

Swipe to start coding

Solution

function ninjaController(ninja) {
ninja.goLeft();
const left = caclulateColumnSize(ninja);
ninja.goRight();
ninja.goRight();

const right = caclulateColumnSize(ninja);
if (left > right) {
ninja.goLeft();
ninja.goLeft();
const sushiToMove = (left - right) / 2;
pickNTop(ninja, left, sushiToMove);

ninja.goRight();
ninja.goRight();
putNTop(ninja, right);
} else {
const sushiToMove = (right - left) / 2;
pickNTop(ninja, right, sushiToMove);

ninja.goLeft();
ninja.goLeft();

putNTop(ninja, left);
}
}

function putNTop(ninja, n) {
while (ninja.objectUp() === "sushi") {
ninja.goUp();
}
for (let i = 0; i < n; i++) {
ninja.putSushi();
ninja.goUp();
}
}

Explore other courses in Catalog

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 6. Chapter 4
js

ninja.js

function ninjaController(ninja) {
// Write your code below


}

Ask AI

expand
ChatGPT

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

some-alt