solution for prob 5, javascript