Lesson 3
JavaScript Journey: Looping Through the Cosmos with Conditional Statements
Introduction and Overview

Hello, future programmer! Today, we're exploring loops and conditional statements in JavaScript. Picture yourself as a space captain counting spaceships and checking fuel levels — that's the scenario we'll simulate with JavaScript loops and conditionals.

Interweaving 'for' Loop with Conditional Statements

In programming, we frequently use loops and conditionals to execute tasks until a certain condition is met. Imagine you are leading a spaceship fleet and need to analyze each ship's fuel status before launching into the cosmos. That’s precisely the point where we would combine JavaScript loops with conditional statements to accomplish the task.

for loops are as dependable as our trusty spaceships. They let us traverse and scrutinize our code universe with precision. Now, we'll apply a for loop and a conditional statement to inspect the passengers' boarding passes before they embark on our spaceship!

JavaScript
1// List of passengers (true means the pass is valid, false means not) 2const passengersBoardingStatus = [true, true, false, true, false]; 3 4// A 'for' loop to check each passenger 5for (let i = 0; i < passengersBoardingStatus.length; i++) { 6 // 'if-else' statement to validate the boarding pass 7 if (passengersBoardingStatus[i]) { 8 console.log('Passenger', i + 1, ': Pass valid.'); 9 } else { 10 console.log('Passenger', i + 1, ': Pass not valid.'); 11 } 12} 13/* 14Prints: 15Passenger 1 : Pass valid. 16Passenger 2 : Pass valid. 17Passenger 3 : Pass not valid. 18Passenger 4 : Pass valid. 19Passenger 5 : Pass not valid. 20*/

In this case, we used a for loop to inspect each passenger's boarding pass!

Joining 'while' Loop with Conditional Statements

Our next stop is while loops! They're like a loyal operations officer, waiting patiently until every astronaut is onboarded on the spaceship. Let's tie this concept up with a while loop and a conditional statement.

JavaScript
1// List of passengers (true means onboard, false means not aboard) 2const passengersOnboardStatus = [true, true, false, true, false]; 3 4// Here, 'i' represents the number of passengers checked 5let i = 0; 6 7// A 'while' loop to check each passenger 8while (i < passengersOnboardStatus.length) { 9 // An 'if-else' condition inside our loop 10 if (passengersOnboardStatus[i]) { 11 console.log('Passenger', i + 1, ': Onboard.'); 12 } else { 13 console.log('Passenger', i + 1, ': Not onboard.'); 14 } 15 i++; // 'i' is increased by 1 for each passenger checked 16} 17/* 18Prints: 19Passenger 1 : Onboard. 20Passenger 2: Onboard. 21Passenger 3 : Not onboard. 22Passenger 4 : Onboard. 23Passenger 5 : Not onboard. 24*/

Our while loop continuously checks the onboard status of each passenger until everyone is accounted for.

Lesson Summary and Practice Exercises

Warp speed ahead, cadet! Today's cosmic journey led us to marry loops with conditionals, thereby strengthening your JavaScript control flow know-how. Now, you're ready to implement what you've learned in the forthcoming practice tasks. As we steer towards the deeper realms of coding cosmos, remember, practice is your guide in this exhilarating voyage! Let's race with the stars, cadet!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.