a solution in javascript