Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements The Conditional Challenge

Dan Fris
Dan Fris
918 Points

My Quiz. What could I have done better?

https://w.trhou.se/oqrdodlens

Let me know what you guys think!

You can try creating a function that will run through an array of all the questions rather than the current code which you repeat alot of the same steps over and over.

3 Answers

Andrew Rodman
Andrew Rodman
9,681 Points

I would say to try to follow D.R.Y. principles (Do Not Repeat Yourself). As mentioned above, you could create a function that loops through an array, vs repeating yourself a bunch of times.

After thinking about how I would create a D.R.Y version of this quiz I came up with this. It's lengthy compared to your's but I remove a lot of repeated steps which is always a best practice.

What do you think? JsFiddle

So here's basically how I did it. The questions are rubbish but it gives you a good idea.

https://w.trhou.se/52lavd1wlr

If you need to compare the answers to another list of correct answers, you need to create another array of "answers" and run through that every time in the loop. So in the if statement that would be if (questions[x] === answers[x]) or something like that.

The only thing I didn't figure out (I'm not a very experienced programmer) is how I could include the prompts in the array right away without having to type them separately. I tried creating a list but something must be wrong with my syntax. Anyway, try using this method, it saved me a lot of typing. And of course, so did the rubbish questions. But it's for demonstrative purposes.