Skip to content

Instantly share code, notes, and snippets.

@vinaydotblog
Created October 8, 2016 04:49
Show Gist options
  • Save vinaydotblog/1eb7ab6dfe4a3314870bcf387da21951 to your computer and use it in GitHub Desktop.
Save vinaydotblog/1eb7ab6dfe4a3314870bcf387da21951 to your computer and use it in GitHub Desktop.
Fixed Point Number 9 as explained by vSouce https://www.youtube.com/watch?v=csInNn6pfT4
function fp(num)
{
// Typecast the user input
num = parseInt(num) || 0;
// Don't accept if length is 1
if( num < 10 ){
throw new Error('You must pass a number of at least two digits');
}
// Add it's digits
while(num > 9) {
let sum = (num + '').split('').map(parseFloat).reduce( (a,b) => a + b );
console.log('%d - %d => %d', num, sum, num - sum);
num = num - sum;
}
return num;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment