Skip to content

Instantly share code, notes, and snippets.

@vasco3
Last active September 7, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vasco3/3c584de00af4a1e4b824 to your computer and use it in GitHub Desktop.
Save vasco3/3c584de00af4a1e4b824 to your computer and use it in GitHub Desktop.
Square root with recursion
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