Updated function
This commit is contained in:
@ -5,10 +5,9 @@ Checks if the given number is an armstrong number or not.
|
|||||||
Convert the given number into array of digits. Use `Math.pow()` to get the appropriate power for each digit and sum them up. If the sum is equal to the number itself, return `true` otherwise `false`.
|
Convert the given number into array of digits. Use `Math.pow()` to get the appropriate power for each digit and sum them up. If the sum is equal to the number itself, return `true` otherwise `false`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const isArmstrongNumber = digits => {
|
const isArmstrongNumber = num => {
|
||||||
let total = 0, arr = (digits+"").split("");
|
let arr = (num+"").split("");
|
||||||
arr.map(d => total += Math.pow(parseInt(d), arr.length))
|
return arr.reduce((a, d) => parseInt(a) + Math.pow(parseInt( d ), arr.length), 0) == num ? true : false
|
||||||
if(total === digits) return true; return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// isArmstrongNumber(1634) -> true
|
// isArmstrongNumber(1634) -> true
|
||||||
|
|||||||
Reference in New Issue
Block a user