Rewrite of ntris in Javascript