ex. 6-8 in JavaScript