Problem_1 solved in javascript with functional style