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 Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1

Can anyone provide feedback on my code?

var questionsAndAnswers =[
  ["What is the capital of England?" , "london"],
  ["What is the finnish word for cheese?" , "juusto"],
  ["What is 2+3?" , "5"]
];


var correct = 0;
var incorrect = 0;

var correctArray = []; //This array stores the questions answered correctly
var incorrectArray = []; //This array stores the questions answered incorrectly

var totalQuestions = questionsAndAnswers.length; //This variable stores the total number of questions within the 2D array


for (var i=0; i<questionsAndAnswers.length; i+=1) {

  var answer = prompt(questionsAndAnswers[i][0]);

  if(answer.toLowerCase() === questionsAndAnswers[i][1]) {

    correct +=1;
    correctArray.push(questionsAndAnswers[i][0]);

  } else {
    incorrect +=1;
    incorrectArray.push(questionsAndAnswers[i][0]);
         }

}


function printArray (arrayName) {

  for (var i=0 ; i<arrayName.length ; i+=1) {
    var brackets = i+1;
    document.write("<p>" + "  " + brackets + ") " + arrayName[i] + "</p>");
  }
}


document.write("<p>You got " + correct + " out of " + totalQuestions + " questions correct!</p>");

document.write("<p>The questions you got correct were: </p>");

printArray(correctArray);

document.write("<p>The questions you got incorrect were: </p>");
printArray(incorrectArray);

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Personally, I'm not sure there is anything I would change. The code works nicely and is DRY. Adding the printout of the actual questions one got wrong and one got right was a very nice touch.

I'd have to say Nice Job! :thumbsup:

:)

Great job. As for changes I would make. Personally I would get rid of 2 variables, totalQuestions and bracket. As you only use them once in your code. I would remove.

Lastly, I would add conditional statements that would only display the correct or incorrect questions IF there were any. Currently if you answer all questions correctly, you still see the incorrect questions p tag and vise versa.

Otherwise, Great job.