review Javascript Algorithm