Day 4: Loops in JavaScript
I am a passionate web developer and open-source enthusiast on a captivating journey of coding wonders. With a year of experience in web development, my curiosity led me to the enchanting world of React, where I found a true calling. Embracing the magic of collaboration and knowledge-sharing, I ventured into the realm of open source, contributing to Digital Public Goods (DPGs) for the betterment of the digital universe. A firm believer in learning in public, I share my insights and discoveries through blogging, inspiring fellow coders to embark on their own magical coding odysseys. Join me on this thrilling adventure, where imagination and technology converge, and together, let's shape the future of the digital landscape! 🎩✨
Screenshot:-

Loops are a fundamental concept in programming, allowing us to execute a block of code multiple times. In this article, we will explore different types of loops in JavaScript, including for loops, while loops, do...while loops, nested loops, and loop control statements.
Activity 1: For Loop
Task 1: Print Numbers from 1 to 10
for (let i = 1; i <= 10; i++) {
console.log(i);
}
This program uses a for loop to print numbers from 1 to 10. The loop starts with i set to 1 and increments i by 1 until it reaches 10.
Task 2: Print the Multiplication Table of 5
for (let i = 1; i <= 10; i++) {
console.log(`5 x ${i} = ${5 * i}`);
}
This program uses a for loop to print the multiplication table of 5. The loop multiplies 5 by each number from 1 to 10 and logs the result.
Activity 2: While Loop
Task 3: Calculate the Sum of Numbers from 1 to 10
let sum = 0;
let num = 1;
while (num <= 10) {
sum += num;
num++;
}
console.log("Sum:", sum);
This program calculates the sum of numbers from 1 to 10 using a while loop. It starts with num set to 1 and adds num to sum until num exceeds 10.
Task 4: Print Numbers from 10 to 1
let count = 10;
while (count >= 1) {
console.log(count);
count--;
}
This program prints numbers from 10 to 1 using a while loop. It starts with count set to 10 and decrements count by 1 until it reaches 1.
Activity 3: Do...While Loop
Task 5: Print Numbers from 1 to 5
let num = 1;
do {
console.log(num);
num++;
} while (num <= 5);
This program uses a do...while loop to print numbers from 1 to 5. The loop executes the code block first and then checks the condition.
Task 6: Calculate the Factorial of a Number
let number = 5; // You can change this number
let factorial = 1;
let i = 1;
do {
factorial *= i;
i++;
} while (i <= number);
console.log(`Factorial of ${number} is ${factorial}`);
This program calculates the factorial of a number using a do...while loop. It multiplies factorial by i until i exceeds the given number.
Activity 4: Nested Loops
Task 7: Print a Pattern
let rows = 5;
for (let i = 1; i <= rows; i++) {
let pattern = "";
for (let j = 1; j <= i; j++) {
pattern += "*";
}
console.log(pattern);
}
This program uses nested for loops to print a pattern of asterisks. The outer loop iterates through the rows, and the inner loop builds the pattern for each row.
Activity 5: Loop Control Statements
Task 8: Skip the Number 5
for (let i = 1; i <= 10; i++) {
if (i === 5) {
continue;
}
console.log(i);
}
This program prints numbers from 1 to 10 but skips the number 5 using the continue statement. When i equals 5, the continue statement skips the current iteration.
Task 9: Stop the Loop at Number 7
for (let i = 1; i <= 10; i++) {
if (i === 7) {
break;
}
console.log(i);
}
This program prints numbers from 1 to 10 but stops the loop when the number is 7 using the break statement. When i equals 7, the break statement terminates the loop.