Mission 2: The Debris Field
The drone must be able to react to its environment. Its forward sensor will report if there is an obstacle.
00:00
Mission Objective
Given `let obstacleDetected = true;`, write an `if...else` statement. If `obstacleDetected` is true, log "Turning left". Otherwise, log "Moving forward".
Step 1 / 3
System Briefing: Logic and Resilience
Spark-1 is active, but it's 'dumb.' You need to give it logic to make decisions and handle unexpected problems as it navigates its first test environment, a simulated debris field.
Loop Selection Protocol
Choosing the right loop for a task is crucial for efficiency and readability.
Loop Type | Syntax | Primary Use Case |
---|---|---|
for | for (let i=0; i<10; i++) | When you know the exact number of iterations needed. |
while | while (condition) | When you want a loop to run as long as a condition is true, but you don't know how many times. |
do...while | do { ... } while (condition) | Same as while, but guarantees the code block runs at least once. |
for...of | for (const item of iterable) | The best way to iterate over the values of an iterable (Array, String, Map). |
for...in | for (const key in object) | To iterate over the property names (keys) of an object. |