CS 101 question solved in JS.