Last active
September 7, 2015 14:20
-
-
Save vasco3/3c584de00af4a1e4b824 to your computer and use it in GitHub Desktop.
Square root with recursion
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 sqrt(original, rootn) { | |
if (rootn < 1) return; | |
rootn = rootn || (original - 1); | |
var isSqRoot = (rootn * rootn) === original; | |
while (isSqRoot === false) { | |
return sqrt(original, rootn - 1); | |
} | |
console.log(rootn); | |
return rootn; | |
} | |
console.log(sqrt(9) === 3); | |
console.log(sqrt(4) === 2); | |
console.log(sqrt(100) === 10); | |
console.log(sqrt(10) === 10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment