how to write algorithms in JavaScript