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?
1 for (let i = 0; i < 100; i++){
2 console.log("Repetition is a good thing.");
3 }
Loops simplify repetitive tasks!
for
Practice¶Construct for
loops that accomplish the following tasks:
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:
Construct a for
loop that sorts the array
[2, 3, 13, 18, -5, 38, -10, 11, 0, 104]
into two new arrays:
evens
array to hold the even numbers and an odds
array for the odd numbers.evens
or odds
, as appropriate.evens
first. Example:
console.log(evens);
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:
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!"
Otherwise add, "Failed to reach orbit."