JavaScript Narcissistic number

In this article, we will learn to check if a number is Narcissistic in JavaScript. A Narcissistic number is a number that equals the sum of its digits, each raised to the power of the total number of digits. We will explore two approaches to solve this problem: an iterative method and a concise string manipulation technique.

What is a Narcissistic Number?

A narcissistic number (also known as an Armstrong number) in a given number base is a number that equals the sum of its digits each raised to the power of the number of digits.

For example:

153 = 1³ + 5³ + 3³ = 1 + 125 + 27 = 153

Similarly:

1 = 1¹ = 1
9474 = 9? + 4? + 7? + 4? = 6561 + 256 + 2401 + 256 = 9474

Different Approaches

Following are the different approaches to check if a number is Narcissistic in JavaScript:

Using Iterative Approach

This approach breaks down the problem step by step. It first calculates the total number of digits in the number, then processes each digit to compute the sum of their powers. Finally, it compares this sum to the original number.

Following are the steps to check if a number is Narcissistic using an iterative approach:

  • Count Digits: Use a while loop to divide the number by 10 until it becomes less than 1. Each division increments the count of digits.
  • Sum the Powers: Extract each digit using the modulo operator % and raise it to the power of the digit count. Accumulate the results.
  • Compare: Check if the sum equals the original number. If true, the number is Narcissistic.

The core calculation uses Math.pow and Math.floor methods:

sum += Math.pow(temp % 10, count);
temp = Math.floor(temp / 10);

Example

Below is an example to check if a number is Narcissistic using an iterative approach:

const isNarcissistic = (num) => {
   let m = 1, count = 0;
   while(num / m > 1){
      m *= 10;
      count++;
   }
   let sum = 0, temp = num;
   while(temp){
      sum += Math.pow(temp % 10, count);
      temp = Math.floor(temp / 10);
   }
   return sum === num;
};

console.log(isNarcissistic(153));
console.log(isNarcissistic(1634));
console.log(isNarcissistic(1433));
console.log(isNarcissistic(342));
true
true
false
false

Time Complexity: O(n), where n is the number of digits in the number. The while loops process each digit.
Space Complexity: O(1), as no additional data structures are used.

Using String Manipulation

This approach takes a more concise and expressive route, focusing on simplifying the process. It directly works with the digits of the number, computes their powers, and accumulates the sum in a streamlined manner.

Following are the steps to check if a number is Narcissistic using string manipulation:

  • Convert to String: Convert the number into a string and split it into an array of characters representing digits using the String.split() method.
  • Calculate Power: Use the array's length property to determine the number of digits.
  • Sum the Powers: Use the reduce() method to iterate over the digits. Convert each digit back to a number, raise it to the power of the digit count, and accumulate the results.
  • Compare: Return true if the sum matches the original number.

Example

Below is an example to check if a number is Narcissistic using string manipulation:

const isNarcissistic = (num) => {
   const digits = String(num).split("");
   const power = digits.length;
   const sum = digits.reduce((acc, digit) => acc + Math.pow(Number(digit), power), 0);
   return sum === num;
};

console.log(isNarcissistic(153));
console.log(isNarcissistic(1634)); 
console.log(isNarcissistic(1433));
console.log(isNarcissistic(342));
true
true
false
false

Time Complexity: O(n), where n is the number of digits. The reduce() method processes each digit.
Space Complexity: O(n), due to the string representation of the number and the split array.

Comparison

Aspect Iterative String Manipulation
Readability Moderate High
Performance Faster (no extra arrays) Slightly slower (due to string conversion)
Space Usage Lower (constant space) Higher (temporary arrays)
Best For Performance-critical applications Code readability and maintainability

Conclusion

Both approaches effectively identify Narcissistic numbers. The iterative method is more memory-efficient, while the string manipulation approach offers cleaner, more readable code. Choose based on your performance requirements and coding preferences.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2026-03-15T23:18:59+05:30

921 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements