trying to make a javascript version. still buggy, won't converge on a result, not sure why