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 trialJeremiah Allen
14,627 PointsBuild a Quiz Challenge: My Code Works (fist pump) but I made something very different than Dave's solution.
Don't mind the ridiculous questions (a couple Idiocracy references; making it fun for myself).
I did a couple of extra things but feel like I can't wrap my head around his answer. I'm super pumped about writing a working program (very new to this) but not sure if I did was I was supposed to.
function print(message) {
document.write(message);
}
var qAnda = [
["When did columbus sail the ocean blue?", '1492'],
["What do plants crave", "electrolytes"],
["What fictional pimp purports a double-dose of pimpin'?", "upgrayedd"]
];
var userAnswer;
var correctNum = 0;
var incorrectNum = 0;
var correctLog = '';
var incorrectLog= '';
for (var i = 0; i < qAnda.length; i +=1 ) {
userAnswer = prompt(qAnda[i][0]);
if (qAnda[i][1] === userAnswer.toLowerCase()) {
correctNum +=1;
correctLog += ((i + 1) + ". " + qAnda[i][0] + " " + userAnswer + "<br>");
} else {
incorrectNum +=1;
incorrectLog += ((i + 1) + ". " + qAnda[i][0]+ " " + userAnswer + " The correct answer was " + qAnda[i][1] + "<br>")
}
}
document.write("<h2>You answered " + correctNum + "/" + qAnda.length + " questions correctly<h2>");
document.write(correctLog);
document.write("<h2> and " + incorrectNum + " incorrectly<h2>");
document.write(incorrectLog);
Any feedback (or words of encouragement) is greatly appreciated. This took me a few hours to solve.
4 Answers
Jason Anders
Treehouse Moderator 145,860 PointsHey there Jeremiah,
First off Great Job!!
It's been a while since I've worked with JavaScript, but I ran your code and it ran just as I thought it should. It doesn't matter how 'the same" or how 'different' it is from Dave's (or anyone else for that matter). Often, there are more than one way of coding the same thing.
As I've said, its be a bit, but you code looks very clean and efficient. I, personally, don't see anything that could be done to make it more DRY or more effective for its purpose.
Good Job and Keep Coding!
Diana McKinlay
5,133 PointsI'm just learning myself but just wanted to say yours looks great, very easy to read and understand. Mainly though I wanted to say I love the questions and answers :)
Jeremiah Allen
14,627 PointsThanks. Haha ;)
gary peart
6,496 PointsYeah, got to say when I did this challenge yesterday it was a real fist pump moment for me when I realised my code actually did the job asked. Hopefully there will be many more fist pump moments to come :)
Joseph Lander
Full Stack JavaScript Techdegree Graduate 27,765 PointsNIce one! (belated)
Like you, mine worked too but I wasn't sure it was best practice either. Watching Dave's I realised that I didn't make variables for my question and answer, I just kept it as the code stated questions[i][0]
. Also I used the print function a few times at the end when I see Dave built his up in stages (stopping a need for text wrapping) before printing. I certainly didn't think to create another function to build a list either.
I've started to look for books to help with how to code clean and funnily enough there are books with that very title. I've bought one, and although it seems to be stating the obvious, it focuses on how to name functions well, sizes of variable names that work best and discusses the creation of functions with single tasks to make reading of your work much easier. This last point stuck with me.
I can see that when I came back to my code only 2 hours later, it didn't immediately make as much sense as it did when I was in the flow. When I look at a chunk of code and it is completing about 6 different things it makes my head spin. I can see why even if sometimes it appears longer, creating blocks with one degree of complexity makes easier reading. A bit like the difficulty I have when trying to read these arrays when they have arrays in side them too!
Jeremiah Allen
14,627 PointsJeremiah Allen
14,627 PointsThank you, and thanks for the advice. This was a very frustrating and fun (funstrating?) project but that feeling of accomplishment can't be beat. I have to say I've been working my butt off, but I feel like I'm making very fast progress with Treehouse. Btw, If there are any books on front-end(aside duckett's) you'd recommend please share.