Problem 2 and 3 solved in JavaScript and Python