Loops
Learn Loops in JavaScript
Want to improve this page? Raise an issue on @github.
What's on this section?
- 🔄 Explore different types of loops in JavaScript.
 - 🔍 Understand the syntax and usage of each loop.
 - 💡 Learn how to control the flow of loops.
 - 🚀 Practice your knowledge through assignments related to loops.
 
📺 Watch Now
We hope that you found the tutorial video helpful in understanding the loops in javascript, You can refer this notes 📝 for quick revision.
📝 Study Notes
Question 1: Sum of all natural numbers from 1 to n
function sumOfNaturalNumber(num){
    let sum = 0;
    for(let i=1; i<=num; i++){
        sum = sum + i;
    }
    return sum;
}
 
console.log(sumOfNaturalNumber(5)); // 15
console.log(sumOfNaturalNumber(10)); // 55
console.log(sumOfNaturalNumber(8)); // 36Question 2: Sum of digits of a number
function sumOfDigits(num){
    let sum = 0;
    while(num > 0){
        sum += num%10;
        num = Math.floor(num / 10);
    }
    return sum;
}
 
console.log(sumOfDigits(1287)); // 18Question 3: Count the number of digits of a number
function countDigits(num){
    num = Math.abs(num);
    let count = 0;
    do {
        count++;
        num = Math.floor(num / 10);
    } while (num > 0);
    return count;
}
 
console.log(countDigits(121)); // 3
console.log(countDigits(-1211413131)); // 10Question 4: Check if a number is palindrome
let isPalindrome = function(x) {
    let copyNum = x, reverseNum = 0;
 
    while(copyNum > 0){
        const lastDigit = copyNum % 10;
        reverseNum = reverseNum * 10 + lastDigit;
        copyNum = Math.floor(copyNum / 10);
    }
 
    return x === reverseNum;
};
 
console.log(isPalindrome(121)); // true
console.log(isPalindrome(1234)); // falseQuestion 5: Find nth Fibonacci number
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1.
let fib = function(n) {
    if(n < 2){
        return n;
    }
 
    let prev = 0, curr = 1, next;
    for(let i=2; i<= n; i++){
        next = prev + curr;
        prev = curr;
        curr = next;
    }
    return next;
};
 
// Fibonacci Sequence: 0 1 1 2 3 5 8...
console.log(fib(5)); // 5
console.log(fib(10)); // 55Question 6: Missing Number in an Array
Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
let missingNumber = function(nums) {
    let sum = 0;
    for(let i=0; i<nums.length; i++){
        sum += nums[i];
    }
    return nums.length*(nums.length+1)/2 - sum;
};
 
// One Line Solution: 
let missingNumber = (nums) => nums.length*(nums.length+1)/2 - nums.reduce((acc, num) => num + acc);
 
console.log(missingNumber([3,0,1])); // 2
console.log(missingNumber([9,6,4,2,3,5,7,0,1])); // 8