cleaned and commented chapter javascript