

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
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 ]
- Related Questions & Answers
- Finding Armstrong numbers in a given range in JavaScript
- Armstrong number within a range in JavaScript
- Armstrong Numbers between two integers?
- Sum of prime numbers between a range - JavaScript
- Generating n random numbers between a range - JavaScript
- Java program to print the Armstrong numbers between two numbers
- Returning array of natural numbers between a range in JavaScript
- Prime numbers in a range - JavaScript
- JavaScript - Accept only numbers between 0 to 255 range?
- C Program for Armstrong Numbers
- Java Program to Display Armstrong Numbers Between Intervals Using Function
- Prime numbers within a range in JavaScript
- Generate n random numbers between a range and pick the greatest in JavaScript
- How to generate armstrong numbers in Python?
- Finding sequential digit numbers within a range in JavaScript
Advertisements