Exercise Solutions: Loops¶
for
Practice¶
Construct
for
loops that accomplish the following tasks:Print the numbers 0 - 20, one number per line.
1 2 3
for (let i = 0; i <= 20; i++) { console.log(i); }
Print the EVEN numbers 12 down to -14 in descending order, one number per line.
1 2 3
for (let i = 12; i >= -14; i-=2) { console.log(i); }
Initialize two variables to hold the string
'LaunchCode'
and the array[1, 5, 'LC101', 'blue', 42]
, then constructfor
loops to accomplish the following tasks:1 2
let str = 'LaunchCode'; let arr = [1, 5, 'LC101', 'blue', 42];
Print each element of the array to a new line.
1 2 3
for (let i = 0; i < arr.length; i++) { console.log(arr[i]); }
Construct a
for
loop that sorts the array[2, 3, 13, 18, -5, 38, -10, 11, 0, 104]
into two new arrays:Define an empty
evens
array to hold the even numbers and anodds
array for the odd numbers.
1 2
let otherArr = [2, 3, 13, 18, -5, 38, -10, 11, 0, 104]; let evens = [], odds = [];
Print the arrays to confirm the results. Print
evens
first. Example:console.log(evens);
1 2
console.log(evens); console.log(odds);
while
Practice¶
Define three variables for the LaunchCode shuttle---one for the starting fuel level, another for the number of astronauts aboard, and the third for the altitude the shuttle reaches.
Construct
while
loops to do the following:Prompt the user to enter the starting fuel level. The loop should continue until the user enters a positive value greater than 5000 but less than 30000.
1 2 3 4 5 6
const input = require('readline-sync'); let fuelLevel = 0, numAstronauts = 0, altitude = 0; while (fuelLevel <= 5000 || fuelLevel > 30000 || isNaN(fuelLevel)) { fuelLevel = input.question("Enter the starting fuel level: "); }
Use a final loop to monitor the fuel status and the altitude of the shuttle. Each iteration, decrease the fuel level by 100 units for each astronaut aboard. Also, increase the altitude by 50 kilometers. (Hint: The loop should end when there is not enough fuel to boost the crew another 50 km, so the fuel level might not reach 0).
1 2 3 4
while (fuelLevel-100*numAstronauts >= 0) { altitude += 50; fuelLevel -= 100*numAstronauts; }
After the loops complete, output the result with the phrase,
The shuttle gained an altitude of ___ km.
If the altitude is 2000 km or higher, add "Orbit achieved!"
1 2 3 4 5
let output = `The shuttle gained an altitude of ${altitude} km.`; if (altitude >= 2000) { output += " Orbit achieved!"; }