JS Generator – Newton’s Method
Basic implementation of the Newton’s Method for narrowing in on a square root. NOTE: in Firefox the yield keyword doesn’t exist unless the script tag loading the function has a type of “text/javascript;version=1.7”. version must be 1.7 or higher.
function newton(value, guess) { value = Math.abs(value); guess = Math.abs(guess); while (1) { guess = guess - (((guess * guess) - value) / (2 * guess)); yield guess; } } var g = newton(83521, 190); for (var i = 0; i < 10; i++) { document.write(g.next() + "<br />"); }