In this tutorial, you’ll learn how to write a JavaScript program to check if Armstrong number. Before getting started you need to know what is an Armstrong number.

What Is An Armstrong Number ?

abcd... = a^n + b^n + c^n + d^n + ...
where n is the number of digits in the number

As seen in the above series, a number whose sum of individual digits power the length of number is equal to the number is called an Armstrong number. An example of Armstrong number is

153 = 1^3 + 5^3 + 3^3
153 = 153

JavaScript Program To Check If Armstrong Number

We’ll first split the passed in number into an array of numbers so that we can iterate over it. While iterating over the number we’ll add the digits power the length of number and at the end compare the sum and the number.

function isArmstrong(num) {
  let digits = [...num.toString()]
  let result = 0
  let power = digits.length;
  digits.forEach((digit) => {
    result+=Math.pow(parseInt(digit),power)
  })
  if(result === num) return "Is Armstrong"
  return "Not Armstrong"
}

console.log(isArmstrong(1634))