Last active
January 29, 2019 15:43
-
-
Save fleepgeek/acf61e5b1e4ea6e78bc171d919708705 to your computer and use it in GitHub Desktop.
A JavaScript function to check if a given number is an Armstrong number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isArmstrong(num){ | |
var numStr = num.toString(); | |
var isArmstrongNum = false; | |
var sum = 0; | |
if(numStr.length === 3){ | |
for(var i=0; i < numStr.length; i++) { | |
var n = parseInt(numStr.charAt(i)); | |
sum += Math.pow(n, 3); | |
} | |
if(sum === num) { | |
isArmstrongNum = true; | |
} | |
} | |
return isArmstrongNum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment