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 trialBozena Jablonski
4,944 PointsHow can I improve my code for Build a Quiz Challenge 1?
1 Answer
Cory Harkins
16,500 PointsHi 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
4,944 PointsBozena Jablonski
4,944 PointsThank you, that makes a lot of sense!