solved problem 18 in javascript