Created
June 15, 2020 02:11
-
-
Save FelixLuciano/a6249f04ede1317ce48be3ab29593c17 to your computer and use it in GitHub Desktop.
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 rationalize (value) { | |
let numerator = 0 | |
let denominator = 1 | |
while (value != numerator / denominator) { | |
const ratio = numerator / denominator | |
if (ratio < value) numerator++ | |
else if (ratio > value) denominator++ | |
else break | |
} | |
return [ numerator, denominator ] | |
} | |
// rationalize(3.14) | |
// -> [ 157, 50 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment