Tricky CS programming question answered in javascript