jshint fixes and refactor of problem 2 by daniele