Some basic algorithms in JavaScript