Last active
May 12, 2020 20:13
-
-
Save ObsidianCat/6b6cc966f77897dd19da2a4e7c5129ea to your computer and use it in GitHub Desktop.
Can be used as foundation for algo questions which ask to split number into digits and manipulate it in some way afterward
This file contains hidden or 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
// Can be used as foundation for algo questions which ask to split number into digits and manipulate it in some way afterward | |
function numberToSumOfDigits(num) { | |
let totalSum = 0; | |
while(num > 0){ | |
const digit = num %10 | |
num = Math.floor(num/10); | |
totalSum += digit | |
} | |
return totalSum | |
} | |
function reverseNumber (num) { | |
const isPositive = (num > 0); | |
num = Math.abs(num); | |
let result = 0; | |
while(num > 0){ | |
const mod = Math.floor(num %10); | |
num = Math.floor(num/10); | |
result = result*10+mod | |
} | |
return isPositive? result: result - (result*2) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment