JavaScript(JS) JS find armstrong number in an interval

https:‮‬//www.theitroad.com

An Armstrong number is a number that is equal to the sum of its digits raised to the power of the number of digits. For example, 153 is an Armstrong number because it is equal to 1^3 + 5^3 + 3^3 = 153.

To find Armstrong numbers in a given interval in JavaScript, you can use a loop to iterate over all the numbers in the interval and check if each one is an Armstrong number. Here's an example:

function findArmstrongNumbers(start, end) {
  let armstrongNumbers = [];
  
  for (let i = start; i <= end; i++) {
    let sum = 0;
    let number = i;
    let numDigits = number.toString().length;

    while (number > 0) {
      let digit = number % 10;
      sum += Math.pow(digit, numDigits);
      number = Math.floor(number / 10);
    }

    if (sum === i) {
      armstrongNumbers.push(i);
    }
  }

  return armstrongNumbers;
}

console.log(findArmstrongNumbers(100, 999)); // Output: [153, 370, 371, 407]

In the example above, we define a function findArmstrongNumbers that takes two arguments start and end, which represent the starting and ending numbers of the interval to search. The function initializes an empty array armstrongNumbers to store the Armstrong numbers found in the interval.

The function then uses a for loop to iterate over all the numbers in the interval from start to end. For each number, it calculates the sum of its digits raised to the power of the number of digits using a while loop. If the sum is equal to the original number, then the number is an Armstrong number, and it is added to the armstrongNumbers array.

Finally, the function returns the armstrongNumbers array, which contains all the Armstrong numbers found in the interval.

In the example above, we call the findArmstrongNumbers function with the arguments 100 and 999, which searches for Armstrong numbers in the interval from 100 to 999. The function returns the array [153, 370, 371, 407], which are the Armstrong numbers in that interval.