finished problem 2 using Js