Dive Into the World of JavaScript Loops
JavaScript Loops
https://basescripts.com/dive-into-the-world-of-javascript-loops-2
Exercise 1: Basic For Loop
Problem: Print numbers 1 through 10 using a for loop.
Explanation: This exercise demonstrates the basic structure of a for loop, which includes initialization, condition, and increment sections.
Code:
for (let i = 1; i <= 10; i++) {
?console.log(i);
}
Exercise 2: For Loop Array Iteration
Problem: Iterate over an array containing ["apple", "banana", "cherry"] and print each fruit.
Explanation: This exercise shows how to use a for loop to iterate through an array's elements.
Code:
let fruits = ["apple", "banana", "cherry"];
for (let i = 0; i < fruits.length; i++) {
?console.log(fruits[i]);
}
Exercise 3: While Loop Basics
Problem: Use a while loop to print numbers from 5 to 15.
Explanation: This exercise introduces the while loop, which continues to execute a block of code as long as a specified condition is true.
Code:
let i = 5;
while (i <= 15) {
?console.log(i);
?i++;
}
Exercise 4: Do-While Loop
Problem: Implement a do-while loop to print "Hello, World!" 5 times.
Explanation: Unlike the while loop, a do-while loop will execute the block of code once before checking if the condition is true, then it will repeat the loop as long as the condition is true.
Code:
let count = 0;
do {
?console.log("Hello, World!");
?count++;
} while (count < 5);
Exercise 5: Breaking Out of a Loop
Problem: Use a for loop to print numbers from 1 to 10. Exit the loop when the loop index is 6 using the break statement.
Explanation: The break statement can be used to exit a loop before it has looped through all items.
Code:
for (let i = 1; i <= 10; i++) {
?if (i === 6) {
?break;
?}
?console.log(i);
}
Exercise 6: Continue in a Loop
Problem: Use a for loop to print all numbers from 1 to 10 except for 4.
Explanation: The continue statement skips one iteration in the loop.
Code:
for (let i = 1; i <= 10; i++) {
?if (i === 4) {
?continue;
?}
?console.log(i);
}
Exercise 7: Nested Loops
Problem: Print a 5x5 grid of asterisks using nested for loops.
Explanation: This exercise demonstrates using one loop inside another to generate a grid pattern.
Code:
for (let i = 0; i < 5; i++) {
?let row = '';
?for (let j = 0; j < 5; j++) {
?row += '* ';
?}
?console.log(row.trim());
}
Exercise 8: Looping Backwards
Problem: Use a for loop to print the numbers 10 down to 1 in reverse order.
Explanation: Looping backwards requires setting the loop's starting condition to the maximum and decrementing the loop variable.
Code:
for (let i = 10; i > 0; i--) {
?console.log(i);
}
Exercise 9: Sum Numbers
Problem: Write a loop that sums numbers from 1 to 100.
Explanation: This shows how to accumulate a total within a loop.
Code:
let sum = 0;
for (let i = 1; i <= 100; i++) {
?sum += i;
}
console.log(sum);
Exercise 10: Factorial Calculation
Problem: Write a function that calculates the factorial of a number using a loop.
Explanation: This demonstrates using a loop within a function to perform a calculation that requires repetitive operations.
Code:
function factorial(n) {
?let result = 1;
?for (let i = 2; i <= n; i++) {
?result *= i;
?}
?return result;
}
console.log(factorial(5)); // Outputs: 120