In this lab, we cover two types of loops:
for-loops and while-loops
Open for_loops.js and while_loops.js in your text editor and solve the challenges listed. Refer to the reference section for help.
Declare a for loop to iterate through each element of an array by accessing it by index
Explain the syntax of a for loop
Search through an array with a while loop
Iterate backwards through an array
Write nested for loops
Debugging for loops
Explain the syntax of a while loop
Debugging while loops
Use readline-sync to take input
Accessing array elements
Writing functions
Using Booleans
String methods
When programming, there are times when the same actions have to be executed a number of times.
For example, if you have an array of names and you want to print out each name in the sentence: "Hi! My name is ___!"
Loops can help you iterate through the array, take each element, and print it in the designated string template.
There are different types of loops:
Loops that execute a specific number of times: For Loops
Loops that run until its told to stop: While Loops
In JavaScript, a for loop begins with the keyword for, followed by three setup statements enclosed in parentheses.
When you know the specific number of iterations you want the loop to execute, you can use a for loop.
Examples of iterations you know:
Length of a string or array
The number of times you want to call a function
The number of times you want to print a value
*Note that the first two statements are followed by a semicolon.
For example, to print each letter of the alphabet you could run the following code:
let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (let i = 0; i < alphabet.length; i++) {
console.log(alphabet.charAt(i));
}
for (let count = 1; count <= 5; count++) {
console.log(`My number is ${count}.`);
}
My number is 1.
My number is 2.
My number is 3.
My number is 4.
My number is 5.
When the loop starts, it initializes the variable count = 1
Is count <= 5 ? True!
Print My number is 1.
Increment count. Now count = 2
Is count <= 5 ? True!
Print My number is 2.
Increment count. Now count = 3
Is count <= 5 ? True!
Print My number is 3.
Increment count. Now count = 4
Is count <= 5 ? True!
Print My number is 5.
Increment count. Now count = 6
Is count <= 5 ? False! 6 > 5
The loop stops running.
let nums = [5, 3, 1, 2, 0]
for (let i= 0; i < nums.length; i++) {
console.log(`I have ${i} lives left.`);
}
I have 5 lives left.
I have 3 lives left.
I have 1 lives left.
I have 2 lives left.
I have 0 lives left.
When the loop starts, it initializes the variable i = 0, since the first index of our array is 0.
Is i < nums.length ? True! The length of the array is 5 and 0 < 5.
Print I have 5 lives left.
Increment i. Now i = 1
Is i < nums.length ? True! 1 < 5
Print I have 3 lives left.
Increment i. Now i = 2
Is i < nums.length ? True! 2 < 5
Print I have 1 lives left.
Increment i. Now i = 3
Is i < nums.length ? True! 3 < 5
Print I have 2 lives left.
Increment i. Now i = 4
Is i < nums.length ? True! 4 < 5
Print I have 0 lives left.
Increment i. Now i = 5
Is i < nums.length ? False! 5 = 5
The loop stops running.
Note that we don't check if i <= nums.length because the last index of an array is 1 less than its length.
In JavaScript, a while loop begins with the keyword while, followed by a logical expression enclosed in parentheses, and ending with a block of code enclosed in curly braces.
When you don't know the specific number of iterations you want the loop to execute, you can use a while loop.
Examples of iterations you don't know:
Number of loops to run to keep a game going until the player loses
Running until a user is able enter a value to meet a condition
Syntactically, while loops are similar to if statements. Think of a while loop as a repeated if statement. The main difference is that the if statement only checks the logical expression in its parentheses once, and the while loop checks it over and over until it is false.
// Let's count to 5!
let counter = 0;
while (counter < =5) {
console.log(counter);
counter++;
}
0
1
2
3
4
5
We initialize our counter variable, counter = 0, outside of the loop so that its scope is outside of the loop. We want to make sure the variable gets updated.
Is counter <= 5 ? True! 0 < 5
Print 0.
Increment counter. Now counter = 1
Is counter <= 5 ? True! 1 < 5
Print 1.
Increment counter. Now counter = 2
Is counter <= 5 ? True! 2 < 5
Print 2.
Increment counter. Now counter = 3
Is counter <= 5 ? True! 3 < 5
Print 3.
Increment counter. Now counter = 4
Is counter <= 5 ? True! 4 < 5
Print 4.
Increment counter. Now counter = 5
Is counter <= 5 ? True! 5 = 5
Print 5.
Increment counter. Now counter = 6
Is counter <= 5 ? False! 6 > 5
The loop stops running.
An infinite loop is a loop that will never ever end!!!
Textbook example (don't do this!):
while (true) {
console.log("I TOLD YOU NOT TO DO THIS!!!");
}
The boolean value true is obviously always going to be true, so this ride will never end!
Beware infinite loops!
You can kill the infinite loop by hitting CTRL + C on your keyboard!