review Algorithm with Javascript