 
  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 numbers between a range - JavaScript
A number is called Armstrong number if the following equation holds true for that number −
xy..z = x^n + y^n+.....+ z^n
Where, n denotes the number of digits in the number
For example − 370 is an Armstrong number because −
3^3 + 7^3 + 0^3 = 27 + 343 + 0 = 370
We are required to write a JavaScript function that takes in two numbers, a range, and returns all the numbers between them that are Armstrong numbers (including them, if they are Armstrong).
Example
Let’s write the code for this function −
const isArmstrong = number => {    let num = number;    const len = String(num).split("").length;    let res = 0;    while(num){       const last = num % 10;       res += Math.pow(last, len);       num = Math.floor(num / 10);    };    return res === number; }; const armstrongBetween = (lower, upper) => {    const res = [];    for(let i = lower; i <= upper; i++){       if(isArmstrong(i)){          res.push(i);       };    };    return res; }; console.log(armstrongBetween(1, 400));  Output
The output in the console: −
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371 ]
Advertisements
 