Javascript solution for Problem 001