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
Nathaniel Nasarow
10,291 PointsJavascript Question challenge. How's my code?
Github links:
Code link: https://github.com/Torgian/question-program
Running program : http://torgian.github.io/question-program
What do you think? I don't know much about arrays, but I'm pretty sure i used it correctly in this case ( var correctAnswers = [] )
Any suggestions on improvements?
1 Answer
Chris Shaw
26,676 PointsHi Nathaniel,
Your code is good but correctAnswers should be an integer instead of an array as you're using it as a counter rather than storing values within it, aside from that the rest of your code looks good.
var correctAnswers = 0;
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 Points+1 Chris.
Nathaniel Nasarow, You can make this code much DRYer, though, and add in support for unlimited questions/answers without taking up a huge amount of space. This may be a little bit beyond your knowledge for right now, but I added in comments that explain everything I did. Notice that I also added in a couple different languages for saying "yes" for the 2nd question that asks "Is Hokkaido in north Japan?", starting with the Japanese formal "hai". You could add in any amount of language converted "yes"s to your question without having to write tons of || logic. And the same goes for your questions.
Nathaniel Nasarow
10,291 PointsNathaniel Nasarow
10,291 PointsMarcus Parsons, Thanks for your answer. This is beyond what I know already, but I recognize the array format. I will study your answer.
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsAbsolutely. That's why I didn't want to post it as an answer because I assumed it was a bit beyond your experience for right now. I added tons of comments though. I also redid the bottom else-ifs after posting to be a more dynamic range and added comments there. It's one of those things that once you start using for loops more, you'll see exactly what I did.
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsOops, I also noticed I had used a variable in my comment that wasn't declared earlier in the comment (but was on my version of the code). I revised it again :)