 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 ]
Advertisements
 