problem 1 solved in javascript