# Armstrong number within a range in JavaScript

Armstrong Numbers: A positive integer is called an Armstrong number (of order n) if −

abcd... = a^n + b^n + c^n + d^n + ...

We are required to write a JavaScript function that takes in an array of exactly two numbers specifying a range.

The function should return an array of all the Armstrong numbers that falls in that range (including the start and end numbers if they are Armstrong).

We will first separately write a function to detect Armstrong numbers and then iterate through the range to fill the array with desired numbers.

## Example

Following is the code −

const range = [11, 1111];
const isArmstrong = (num) => {
const numberOfDigits = ('' + num).length;
let sum = 0;
let temp = num;
while (temp > 0) {
let remainder = temp % 10;
sum += remainder ** numberOfDigits;
temp = parseInt(temp / 10);
}
return sum === num;
};
const findAllArmstrong = ([start, end]) => {
const res = [];
for(let i = start; i <= end; i++){
if(isArmstrong(i)){
res.push(i);
};
};
return res;
};
console.log(findAllArmstrong(range));

## Output

Following is the console output −

[ 153, 370, 371, 407 ]

Updated on: 20-Jan-2021

275 Views

##### Kickstart Your Career

Get certified by completing the course