Created
October 8, 2016 04:49
-
-
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
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
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