Skip to content

Instantly share code, notes, and snippets.

@ObsidianCat
Last active May 12, 2020 20:13
Show Gist options
  • Save ObsidianCat/6b6cc966f77897dd19da2a4e7c5129ea to your computer and use it in GitHub Desktop.
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
// 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