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 Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 2 Solution

Dharma Teegala
Dharma Teegala
4,401 Points

Build a quiz challenge part 2 , written in 33 lines followed DRY concept, without creating 2 arrays please comment

var correctCount= 0;
var wrongCount = 0;
var anwser;
var question;
var response;
var correctAns = '<h2>' + 'You have got these question(s) correct:' + '</h2>' + '<ol>';
var wrongAns = '<h2>' + 'You have got these question(s) wrong:' + '</h2>' + '<ol>';
function print(message) {
  document.write(message);
}
var questions = [
  ['How many states are in the United States?', 50],
  ['How many continents are there?', 7],
  ['How many legs does an insect have?', 6]
];
for ( var i = 0; i < questions.length; i += 1 ) {
  question = questions[i][0];
  anwser = questions[i][1];
  response = prompt(question);
  response = parseInt(response);

if (response === anwser) {
  correctCount += 1;
  correctAns += '<li>'+questions[i][0] + '</li>';

    } else {
  wrongCount +=1;
  wrongAns += '<li>' + questions[i][0] + '</li>';

  }
  } 
correctAns += '</ol>';
wrongAns += '</ol>';
correctAns = 'You got ' + correctCount + ' question(s) right:<br>' + correctAns;
print(correctAns);
print(wrongAns);

Mod Note - Added backticks for additional formatting and syntax highlighting of your code.

2 Answers

Joseph Fraley
STAFF
Joseph Fraley
Treehouse Guest Teacher

Oh yeah, that's pretty slick. Have you played around on Code Wars ? You should! You'll see that on Code Wars, it's possible to vote a puzzle's solution as "clever". This is sort of a back-handed compliment in programming. It usually means that the code is very DRY, even if it requires using parts of the language that are obtuse, or the code is more difficult for humans to read. A "clever" solution means "yeah, that's cool, but I would probably never do this."

When writing DRY code, it's important to remember that the clever answers do not score you points in real life. Often times other people will be reading, proofing, updating, or otherwise trying to improve your code. VERY often that person will be YOU several months after writing it. DRY code is very important, and a good rule of thumb, but it should never come at the expense of being easy to read.

For example:

for ( var i = 0; i < questions.length; i += 1 ) {
  question = questions[i][0];
  anwser = questions[i][1];
  response = prompt(question);
  response = parseInt(response);

is not IMMEDIATELY clear. This code is very tidy and DRY, but it requires the reader to interpret two "at" notation levels, which is a bit confusing. I had to reorient myself -- remind myself that you have a nested array -- and think a little extra about what each [] refers to. Sometimes you want this for performance purposes -- if the code is substantially faster for being shorter. But most of the time, the performance boost isn't worth the confusion you doom yourself to later.

It's great to practice writing super DRY code in these exercises, and in places like Code Wars. It makes you a better programmer! But when writing production code, always prioritize human readability whenever possible!

Does that answer your question? If so, please up-vote my answer as "Best Answer"! It helps me reach my career path goals quickly. If not, feel free to write more!

Dharma Teegala
Dharma Teegala
4,401 Points

It's very informative and a good answer by Joseph Fraley.......Thanks

Joseph Fraley
Joseph Fraley
Treehouse Guest Teacher

Thanks Dharma, I'm glad I could help. You weren't able to up-vote my answer because I mistakenly posted it as a "comment" on your question. Would you please up-vote it now that I've moved it, so that I can make progress in my career track?

Thanks so much! Happy studies!