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.

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1

1 Answer

Cory Harkins
Cory Harkins
16,487 Points

Hi Bozena!

Thank you for sharing your code. It really takes a lot of courage to ask that question, and I want you to know that that is the sign of a great developer. I've looked over your code, and think it looks fine for the context it was written in.

There are a few opinionated tweaks I could offer you, yet, that won't give you anything but my bad habits! LOL

However, here's a section of code you can do something with :)

Your code:

for ( var i = 0; i <= 2; i += 1) {
  askQuestion(i);
}


if (score === 3) {
  print("Congratulations! You finished top of the class with " + score + " points.");
} else if (score === 1 || score === 2) {
  print("Oh well, at least you tried. Let's see if you can get higher than " + score + " points next time.");
} else {
  print("O-oh! You got " + score + " points. I think you need more practice!");
}

My suggested changes:

// asking for the length of questions makes your code scalable, 
// now you can add as many questions as you'd like 
// without having to change the amount of iterations
for ( var i = 0; i < questions.length; i += 1) { 
  askQuestion(i);
}

// scalability
// again, ask for length of questions for a perfect score
if (score === questions.length) {
  print("Congratulations! You finished top of the class with " + score + " points.");

// here I would ask for a percentage, but this is just my opinion really
// You would need to write a condition for >= 80 && < 100
} else if (Math.floor(score/questions.length * 100) < 80) {
  print("Oh well, at least you tried. Let's see if you can get higher than " + score + " points next time.");
} else {
  print("O-oh! You got " + score + " points. I think you need more practice!");
}

Ultimately, your code is fine for now, and as you develop as a developer will eventually find your personal expression in code. Keep up the good work!

Bozena Jablonski
Bozena Jablonski
4,944 Points

Thank you, that makes a lot of sense!