javascript

findNarcissisticNumbersRange()

Parameters: (start: integer, end: integer)

start and end of the range to find narcissistic numbers

Returns: Array of narcissistic numbers within the provided range

This function takes a range of numbers as inputs, and outputs all of the narcissistic numbers inside that range. Great for brushing up on JavaScript and math.

Variables
Loops
Conditionals
Functions
Arrays
Medium dificulty

Finding Narcissistic Numbers in a Range with JavaScript

Greetings, fellow programmer! This article will guide you through the intricacies of implementing a function, find-narcissistic-numbers-range, in JavaScript. Be prepared to dive deep into the world of narcissistic numbers and uncover how they can be harnessed within an array range. It's a fascinating journey that will not only hone your JavaScript skills but also widen your understanding of numerical functions. A gentle warning though, keep your thinking cap on, we're dealing with numbers here. Ready to take the plunge? Let's get coding!

Step 1:

We would start solving the function by first defining our function and creating a for loop that will iterate through the given range of numbers.

function findNarcissisticNumber(n, m) {
  for(var i = n; i <= m; i++) {

  }
}

Step 2:

On this step, we will further proceed by converting each integer to a string and calculating the number of digits in the integer which is simply the length of the string.

function findNarcissisticNumber(n, m) {
  for(var i = n; i <= m; i++) {
    var str = i.toString();
    var noOfDigits = str.length;
  }
}

Step 3:

We will use map function to convert each character in the string to integer and raise it to the power of no of digits in the integer. We will then use reduce function to sum all the values generated from the map function which gives us the resultant sum.

function findNarcissisticNumber(n, m) {
  for(var i = n; i <= m; i++) {
    var str = i.toString();
    var noOfDigits = str.length;
    var sum = Array.from(str).map(number => Math.pow(Number(number), noOfDigits)).reduce((prev, curr) => prev + curr, 0);
  }
}

Step 4:

We will then check if the sum obtained in previous step is equal to the current integer. If it is, we will push that integer to our answer array.

function findNarcissisticNumber(n, m) {
  var result = [];
  for(var i = n; i <= m; i++) {
    var str = i.toString();
    var noOfDigits = str.length;
    var sum = Array.from(str).map(number => Math.pow(Number(number), noOfDigits)).reduce((prev, curr) => prev + curr, 0);
    if(sum === i) {
      result.push(i);
    }
  }
  return result;
}

Conclusion:

Finally, we get our required function which checks for every number in the given range if it's a narcissistic number. If it is, that number is appended to our result list which is then returned by the function. A narcissistic number in a given number base is a number that is the sum of its own digits each raised to the power of the number of digits. This JavaScript function efficiently finds and returns all such numbers in a given range.

Learn function in:

Narcissistic Number in Range

Find narcissistic numbers within a given range

Learn more

Mathematical principle

A narcissistic number in a given number base is a number that is the sum of its own digits each raised to the power of the number of digits. For example, the 3-digit decimal number 153 is a narcissistic number because `153 = 1^3 + 5^3 + 3^3`.

Learn more