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 Solution

All variables before code execution

Hello,

i've created all 5 variables (questions) like this:

// Score
var score = 0;

// Questions
var question1 = prompt('1st question to ask');
var question2 = prompt('2nd question to ask');
var question3 = prompt('3rd question to ask');
var question4 = prompt('4th question to ask?');
var question5 = prompt('5th question to ask?');

// Answer
var answer = 'Yes';

if (question1 === answer) {
  score += 1;
} else {
  score; 
}

if (question2 === answer) {
  score += 1;
} else {
  score; 
}

if (question3 === answer) {
  score += 1;
} else {
  score; 
}

if (question4 === answer) {
  score += 1;
} else {
  score; 
}

if (question5 === answer) {
  score += 1;
} else {
  score; 
}

Is that a bad practice? Putting all variable in one place before the code? Because Dave write those question on each IF like this:

var question1 = prompt('1st question to ask');
if (question1 === answer) {
  score += 1;
} else {
  score; 
}

var question2 = prompt('2nd question to ask');
if (question2 === answer) {
  score += 1;
} else {
  score; 
}

and so on... I don't want to start ma JavaScript journey with bad habits, that's my point here...

Thank you!

1 Answer

Actually, I'd say that is fine. I still would say Dave's way is best practice since it is easiest to understand, but your method isn't considered bad practice. It's a 100% OK to do it your way! :)

Good luck!

Hope this helps, Alex