Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

1 Answer

it is advised that you should not use, or attempt to not use global variables. They can cause problems that can be difficult to find.

The code below is a way that I use that gets around this (i pulled this from another website a while back but sadly I do not have that site for reference).

const myApp = {};
(function() {

put code here

}).apply(myApp);

This acts like a namespace that you would find in Java. It encapsulates everything so that nothing is actually added to the global namespace.

Also, there are several places where you have some serious if statements going on. You should try to refactor your code so that there are not so many embedded if statements. Try going with more function declarations or some other approach. This will help in the maintenance and upkeep of your code. And it will make it easier to read/understand.

Please understand your code is "clean" looking and obviously functional, just a couple things pointed out that I would have tried to do differently.