Try to write a JavaScript implementation