solved problem 6 again in javascript