Solution for problem002 in Javascript