rewrite in javascript