An Armstrong number, also known as a narcissistic number, is a number that is equal to the sum of its own digits each raised to the power of the number of digits in the number. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.

In JavaScript, you can check if a number is an Armstrong number by first getting the number of digits in the number, then using a for loop to iterate through each digit, raising each digit to the power of the number of digits, and adding them together. Finally, you can check if the sum is equal to the original number. Here is an example function:

function isArmstrongNumber(num) {
    var originalNum = num;
    var numOfDigits = num.toString().length;
    var sum = 0;
    while (num > 0) {
        var digit = num % 10;
        sum += Math.pow(digit, numOfDigits);
        num = Math.floor(num / 10);
    }
    return sum === originalNum;
}

You can then call this function and pass in a number to check if it is an Armstrong number, for example:

console.log(isArmstrongNumber(153)); // true (1^3 + 5^3 + 3^3 = 153)
console.log(isArmstrongNumber(1634)); // true (1^4 + 6^4 + 3^4 + 4^4 = 1634)
console.log(isArmstrongNumber(371)); // true (3^3 + 7^3 + 1^3 = 371)
console.log(isArmstrongNumber(407)); // true (4^3 + 0^3 + 7^3 = 407)
console.log(isArmstrongNumber(16)); // false

Also Read:

Categorized in: