LEARN JAVASCRIPT
🚀 Elevate Your JavaScript Skills with Loop Exercises! 💻🔁
Exercise JavaScript Loops!
This series of hands-on coding exercises focused on JavaScript Loops. Whether you’re a coding rookie or a seasoned dev looking to brush up on the fundamentals, these exercises are crafted just for you! 🎯📚
We’ve covered a variety of loop types and scenarios:
- Printing Numbers
- Summation Tasks
- Countdowns with while
- Array Iterations
- Finding Maximum Values
- Solving FizzBuzz
- Using continue for Even Numbers
- Crafting Multiplication Tables
- String Reversals
- Object Property Traversal
Each exercise is accompanied by a solution and a detailed explanation to ensure a deep and clear understanding. 🤓✨
Exercise: Print Numbers from 1 to 10
Problem: Write a JavaScript for loop to print numbers from 1 to 10.
Solution:
for (let i = 1; i <= 10; i++) {
console.log(i);
}
Explanation: The for loop initializes i to 1 and increments it by 1 on each iteration, continuing until i exceeds 10.
Exercise: Sum of First N Natural Numbers
Problem: Use a JavaScript for loop to find the sum of the first N natural numbers.
Solution:
function sumOfN(n) {
let sum = 0;
for (let i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
console.log(sumOfN(10)); // Output: 55
Explanation: The function loops from 1 to n, adding each number to the sum.
Exercise: Count Down Timer
Problem: Create a countdown from a specified number to 1 using a while loop.
Solution:
function countdown(start) {
while (start > 0) {
console.log(start);
start–;
}
}
countdown(5);
Explanation: The while loop continues to execute as long as start is greater than 0, decrementing start in each iteration.
Exercise: Iterate Through an Array
Problem: Use a for loop to iterate through an array and print each element.
Solution:
let fruits = [‘apple’, ‘banana’, ‘cherry’];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
Explanation: The loop iterates over the array indices and prints the corresponding element at each index.
Exercise: Find the Largest Number in an Array
Problem: Write a function using a loop to find the largest number in an array.
Solution:
function findLargest(arr) {
let largest = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
return largest;
}
console.log(findLargest([10, 4, 2, 15, 6])); // Output: 15
Explanation: The loop iterates through the array, updating the largest variable whenever a larger number is found.
Exercise: FizzBuzz Problem
Problem: Implement the FizzBuzz problem using a loop: Print numbers from 1 to 100, but for multiples of 3 print “Fizz,” for multiples of 5 print “Buzz,” and for multiples of both 3 and 5, print “FizzBuzz.”
Solution:
for (let i = 1; i <= 100; i++) {
let output = ”;
if (i % 3 === 0) output += ‘Fizz’;
if (i % 5 === 0) output += ‘Buzz’;
console.log(output || i);
}
Explanation: The loop uses conditional statements to concatenate ‘Fizz’, ‘Buzz’, or both to the output string based on divisibility.
Exercise: Print Even Numbers Using continue
Problem: Use a for loop with the continue statement to print all even numbers from 1 to 20.
Solution:
for (let i = 1; i <= 20; i++) {
if (i % 2 !== 0) {
continue;
}
console.log(i);
}
Explanation: The continue statement is used to skip odd numbers, only printing even ones.
Exercise: Create a Multiplication Table
Problem: Write a JavaScript function to generate a multiplication table for a given number up to 10 using nested loops.
Solution:
function multiplicationTable(number) {
for (let i = 1; i <= 10; i++) {
console.log(`${number} * ${i} = ${number * i}`);
}
}
multiplicationTable(5);
Explanation: The function prints the multiplication table for the provided number using a loop.
Exercise: Reverse a String
Problem: Use a loop to reverse a given string.
Solution:
function reverseString(str) {
let reversed = ”;
for (let i = str.length – 1; i >= 0; i–) {
reversed += str[i];
}
return reversed;
}
console.log(reverseString(‘hello’)); // Output: ‘olleh’
Explanation: The loop iterates over the string in reverse order, concatenating each character to form the reversed string.
Exercise: Iterate Through Object Properties
Problem: Use a for…in loop to iterate through all properties of an object and print them.
Solution:
let person = { name: ‘Alice’, age: 30, job: ‘Developer’ };
for (let key in person) {
console.log(`${key}: ${person[key]}`);
}
Explanation: The for…in loop iterates over each property in the object, accessing both the property name (key) and its value.