JS solution for Problem 8