The problem that you are running into is a "signed integer overflow."
For clarity, I've split the long if statement in your code into two lines.
let xx = Math.multiply((y + twoToThe[j]), (y + twoToThe[j]));
if ( (xx < x) | (xx = x) )
{
When you pass a value greater than or equal to 16384 into your square root function, (y+2^j)^2 can be > 32767 so it is too large to fit in a signed integer so the result is a negative number.
j x y (y+2^j)^2
7 16384 0 16384
6 16384 128 36864 (-28672)
There are two ways to fix your code.
Redesign your algorithm so that it does not cause multiplication overflows. (This can be quite difficult.)
Handle the overflows when they occur. (Much easier.)
You can handle this overflow by checking if the result of the multiply is negative. If it is, then you know that (y+2^j)^2 > x and y should not be adjusted.
let xx = Math.multiply((y + twoToThe[j]), (y + twoToThe[j]));
if (xx < 0) {
// overflow occurred, xx > x so do nothing
} else {
if ( (xx < x) | (xx = x) )
{
Signed integer overflow is beyond the scope of the n2t course, but if you want to read more about it, I wrote
this article that analyses the n2t division algorithm.
--Mark