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. For example, 153 is an Armstrong number because (1^3 + 5^3 + 3^3 = 153). This concept is often utilized in computer science education to help students understand looping and conditional statements in programming.
In this article, you will learn how to create a JavaScript program to check if a given number is an Armstrong number. Discover how to use JavaScript functions, looping, and conditionals through step-by-step examples.
Define a function isArmstrong
that takes a single argument, number
.
Convert the number to a string to manipulate individual digits easily.
function isArmstrong(number) {
let sum = 0;
const numberString = number.toString();
const numberOfDigits = numberString.length;
In this code block, number.toString()
converts the number to a string, allowing for iteration over each digit. numberString.length
gives the count of digits, which is necessary for the power calculation.
Loop through each digit of the number.
Convert each digit back to a number and raise it to the power of numberOfDigits
.
Accumulate the sum of these values.
for (let i = 0; i < numberOfDigits; i++) {
sum += Math.pow(Number(numberString[i]), numberOfDigits);
}
Within the loop, Number(numberString[i])
converts the character back to a numeric value. Math.pow()
then raises this number to the power of numberOfDigits
.
Use an if
statement to compare the computed sum with the original number.
Return true
if they are equal; otherwise, return false
.
return sum === number;
}
This line checks if the calculated sum of powers equals the input number, determining if it is an Armstrong number.
function isArmstrong(number) {
let sum = 0;
const numberString = number.toString();
const numberOfDigits = numberString.length;
for (let i = 0; i < numberOfDigits; i++) {
sum += Math.pow(Number(numberString[i]), numberOfDigits);
}
return sum === number;
}
This full function combines all the snippets provided above, creating a complete program that can be used to check if a number is an Armstrong number.
Call the function with various numbers to see if they are Armstrong numbers.
console.log(isArmstrong(153)); // true
console.log(isArmstrong(370)); // true
console.log(isArmstrong(9474)); // true
console.log(isArmstrong(123)); // false
These test cases cover both true and false examples, demonstrating the function's utility. Use them to validate the functionality of your isArmstrong
function.
You can utilize the isArmstrong
function to determine if a given number is an Armstrong number in JavaScript. This approach provides a clear application of converting a number into a string for digit manipulation, looping through characters, converting them back to numbers, and applying mathematical operations. It's a practical example of using basic mathematics and programming concepts to solve a typical computational problem. Use these techniques to strengthen your understanding of JavaScript and improve your problem-solving skills in programming.