π Practise Loop, Functions & Math Problems in JavaScript
π Loop, Functions & Math

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.



