problem 1 answered in JavaScript