Skip to main content

Command Palette

Search for a command to run...

πŸš€ Practise Loop, Functions & Math Problems in JavaScript

πŸš€ Loop, Functions & Math

Published
β€’3 min readβ€’View as Markdown
πŸš€ Practise Loop, Functions & Math Problems in JavaScript

Loops, functions, and mathematical logic form the backbone of programming. Whether you are preparing for coding interviews or brushing up on fundamentals, these classic problems strengthen your problem-solving skills and help you write efficient JavaScript code.

In this blog, we’ll cover popular loop-based and math-based problems along with explanations and solutions.


βœ… 1. Sum of All Natural Numbers from 1 to n

A natural number series grows linearly, and the simplest approach is using a loop.

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));  // 36

πŸ“Œ Concept: Looping + accumulator pattern
πŸ“Œ Alternative formula: n * (n + 1) / 2


βœ… 2. Sum of Digits of a Number

Extract the last digit using % 10 and remove digits using Math.floor(num / 10).

function sumOfDigits(num){
    let sum = 0;
    while(num > 0){
        sum += num % 10;
        num = Math.floor(num / 10);
    }
    return sum;
}

console.log(sumOfDigits(1287)); // 18

πŸ“Œ Useful for: Digital sums, numeric algorithm problems.


βœ… 3. Count the Number of Digits in 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));     // 10

πŸ“Œ Why do this? Helps in modular arithmetic and numeric pattern detection.


βœ… 4. Check if a Number is Palindrome

A palindrome number remains the same when reversed.

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)); // false

πŸ“Œ Used in: LeetCode, HackerRank and coding interviews.


βœ… 5. Find nth Fibonacci Number

The Fibonacci sequence follows:
0, 1, 1, 2, 3, 5, 8...

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;
};

console.log(fib(5));  // 5
console.log(fib(10)); // 55

πŸ“Œ Efficient iterative solution.
πŸ“Œ No recursion needed.


βœ… 6. Missing Number in an Array

Given an array of numbers from 0 to n, find which one is missing.

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

πŸ“Œ Uses math formula + reduce.


πŸ“ Practice Questions (Try Yourself)

βœ” Count Odd Numbers in an Interval Range
βœ” Fizz Buzz
βœ” Power of Two
βœ” Find Square Root of a Number

πŸ”₯ These problems are great to sharpen your fundamentals before moving to advanced DSA topics!


🎯 Final Thoughts

Practicing loops, math logic, and functions in JavaScript builds a strong foundation that prepares you for real-world coding challenges and technical interviews. Keep solving, keep learning, and your logical thinking will grow rapidly.