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.
Samuel Llibre Santos15,467 Points
My approach on this project.
I can think of 2 or 3 other option to do the same, but I found this the easiest and fastest to write.
You don't have to, but you should always close your element tags. It's best practice to do so as it increases readability and maintainability. Also, if you don't, they'll get popped in a validator or other monitoring tools like SEMRush.
Other than that, the code looks pretty ok! I really like your proper use of h1s and h2s. A few things to improve your code:
- Answer isn't going to change, so that should use the
constkeyword instead of let.
- Instead of constantly referring to
questions[i], just set a const value at the top. This will increase readability and while this is minute, it's good practice. It requires more memory usage to look for a key in an array twice times than it does to do it once and then a locally scoped variable twice.
document.querySelectoris an expensive operation. You have it in a loop, which means it's running 4 times each. It'll be more optimized, and readable, to put it in global scope as a const then reference it the loop. This takes your 12 dom operations and makes it 3.
- For loop is fine, but it's a bit messy syntax. if your only goal is to loop over the array, it would be cleaner to utilize Array.forEach.
- This is more for formatting, but it's important. Take pride in your code. Be sure to use consistent formatting. Having sloppy HTML will get thrown back in a code review every single time and it's really just a lazy thing. Indent properly, have a cohesive strategies for linebreaks etc etc.