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 Arrays Multidimensional Arrays Build a Quiz Challenge – One Solution

Connor Acott
Connor Acott
7,572 Points

Kind of a nit-picky question, but I was curious about best practices with variables.

Something I've been wondering for a little now while during JavaScript courses, and I'd appreciate if anyone wouldn't mind clearing this up.

I solved step 3 (the "for" loop) like this:

for ( let i = 0; i < quiz.length; i++ ) {
  let guess = prompt(quiz[i][0]);
  if ( guess.toLowerCase() === quiz[i][1].toLowerCase() ) {
    totalCorrect++;
  }
}

When watching Guil's solution video however, I noticed that he declared variables for question and answer within his loop first. If I had done the same, my code would have looked like this instead:

for ( let i = 0; i < quiz.length; i++ ) {
  let question = quiz[i][0];
  let answer = quiz[i][1];
  let guess = prompt(question);
  if ( guess.toLowerCase() === answer.toLowerCase() ) {
    totalCorrect++;
  }
}

Is this just a personal preference, or maybe something that Guil is doing to make it easier to follow? Or is it the kind of thing that I should get in the habit of doing too? Even though my code ran fine, I could totally see this being the kind of thing that's encouraged in the professional world to make your code more readable.

If it doesn't matter then I'll stick to doing things the way that's most intuitive for me, but I just want to make sure that I'm not forming bad habits. I've noticed a lot of variables popping up in class that don't seem strictly necessary, so I've been wondering if I missed something about the "right time" to declare a variable. Thank you!

Hi Connor Acott,

Everyone have different approaches for solving a problem.

The way you approached the solution is also fine for this case because you're not using quiz[i][0] and quiz[i][1] in multiple places.

for ( let i = 0; i < quiz.length; i++ ) {
  let guess = prompt(quiz[i][0]);
  if ( guess.toLowerCase() === quiz[i][1].toLowerCase() ) {
    totalCorrect++;
  }
}

The code solution of Guil have few things I like to mention

  1. The variable question and answer help other developer to understand code more fast because they will be able to identify which one index is question and which one is answer.

  2. If code require more use of question and answer instead of using quiz[i][0] or quiz[i][1] one can simply use variable question and answer.

for ( let i = 0; i < quiz.length; i++ ) {
  let question = quiz[i][0];
  let answer = quiz[i][1];
  let guess = prompt(question);
  if ( guess.toLowerCase() === answer.toLowerCase() ) {
    totalCorrect++;
  }
}

My approach for this code will be:

  1. I'm using destructuring assignment for storing question and answer
  2. I'm using const here because I'm assuming there is no need to change values.
for ( let i = 0; i < quiz.length; i++ ) {
  const [question,answer] = quiz[i];
  const  guess = prompt(question);
  if ( guess.toLowerCase() === answer.toLowerCase() ) {
    totalCorrect++;
  }
}

Hope it helps,

Thanks

Connor Acott
Connor Acott
7,572 Points

Awesome, thanks Ashish Mehra ! Makes sense that adding the variables in this case could help readability, but also wouldn't be strictly necessary. Glad to hear I didn't miss anything after all. I think I keep expecting there to be more "rights" and "wrongs" than there actually are when writing code, and it's nice to see that it's generally more open-ended than that. Appreciate the example of a third option too!

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Connor Acott! I could be wrong, but I believe you hit the nail on the head when you wrote "make it easier to follow". I have a strong feeling that that's exactly why Guil chose that. I could be wrong, of course.

There are some cases where readability comes into play, but I don't think your solution is unreadable at all, personally. Just my two cents.

Hope this helps! :sparkles:

Connor Acott
Connor Acott
7,572 Points

Cool, thanks Jennifer Nordell ! Makes sense to me. I feel like I might be expecting there to be more "right" or "wrong" answers than there actually are when writing code, when maybe it's more like "readable = good, confusing = bad, end of story." Glad to hear I wasn't missing something!