Exercises: Loops

Practice makes better. Repetition is a good thing. Repetition is a good thing. Repetition is a good thing. Repetition is a good thing.

WAIT!!! Why type “Repetition is a good thing,” four times when we can code a better result? How about printing the phrase 100 times instead?

for (let i = 0; i < 100; i++){
   console.log("Repetition is a good thing.");

Loops simplify repetitive tasks!

for Practice

Open up for-Loop-Exercises.js in loops/exercises in javascript-projects. This is the file you will use to test your skills with for loops.

  1. Construct for loops that accomplish the following tasks:

    1. Print the numbers 0 - 20, one number per line.
    2. Print only the ODD values from 3 - 29, one number per line.
    3. Print the EVEN numbers 12 down to -14 in descending order, one number per line.
    4. Print the numbers 50 down to 20 in descending order, but only if the numbers are multiples of 3.
    Check your solution
  2. Initialize two variables to hold the string 'LaunchCode' and the array [1, 5, 'LC101', 'blue', 42], then construct for loops to accomplish the following tasks:

    1. Print each element of the array to a new line.
    2. Print each character of the string—in reverse order—to a new line.
    Check your solution
  3. Construct a for loop that sorts the array [2, 3, 13, 18, -5, 38, -10, 11, 0, 104] into two new arrays:

    1. Define an empty evens array to hold the even numbers and an odds array for the odd numbers.
    2. In the loop, determine if each number is even or odd, then put that number into evens or odds, as appropriate.
    3. Print the arrays to confirm the results. Print evens first. Example: console.log(evens);
    Check your solution

while Practice

To practice with while loops, open up while-Loop-Exercises.js in loops/exercises in javascript-projects.

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.

  1. Construct while loops to do the following:

    1. 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.
    2. Use a second loop to query the user for the number of astronauts (up to a maximum of 7). Validate the entry by having the loop continue until the user enters an integer from 1 - 7.
    3. 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).
    Check your solution
  2. After all the loops complete, output the result with the phrase, The shuttle gained an altitude of ___ km.

    1. If the altitude is 2000 km or higher, add “Orbit achieved!”
    2. Otherwise add, “Failed to reach orbit.”
    Check your solution