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

Graeme Oxley
Graeme Oxley
8,931 Points

Code critique please

function print(message) {
  document.write(message);
}

var questionAnswer = [
  ['1?', '1'],
  ['2?', '2'],
  ['3?', '3']
];

var goodAnswer = [];
var badAnswer = [];

var answer;

for (i = 0; i < questionAnswer.length; i += 1) {
  answer = prompt( questionAnswer[i][0] );
  if ( answer === questionAnswer[i][1] ) {
    goodAnswer.push( questionAnswer[i][0] );
  } else {
    badAnswer.push( questionAnswer[i][0] );
  }
}

print("<h1>You got " + goodAnswer.length + " correct.</h1>");

print("<h2>You answered the following questions correctly:</h2></p>" + goodAnswer.join('<br>') + "</p>");

print("<h2>The following questions were answered incorrectly:</h2></p>" + badAnswer.join('<br>') + "</p>");

I was pleasantly surprised to see that I retained enough to write this without notes. Please let me know if there are any areas where I could improve. Thank you.

Code formatting fixed - Please try to format it according the Markdown Cheatsheet next time :)

Graeme Oxley
Graeme Oxley
8,931 Points

Philip Graf , sorry I was unaware of the three backticks for code. This was my first time pasting in code. Thank you for the insight.

No Problem, we are all here to help and learn :)

2 Answers

My only critique after a fast check of your code is: Does it really make your work easier if you add a print function instead of directly typing document.write? Also, instead of i +=1 you could just use i++

Best regards, Philip

Graeme Oxley
Graeme Oxley
8,931 Points

Well the print function is actually the default code in the work space when you start. In this case, of course it doesn't make any difference, but I think the intention (from Treehouse) is to get us in the mindset of using functions to save space instead of repeating code.

As for "i += 1", this is the way it is taught in the videos.

Well, as it is a small piece of code, i took a look at every little detail :) The programmer in me just yelled that it would maybe be a (very) little performance overhead. As this function only calls another one, it's also not used to avoid repeating code. BTW, i++ is the shorthand to i+=1. If you are interested, you can continue reading here

Eduardo Uribe
Eduardo Uribe
1,511 Points

Great job Graeme Oxley... I'm currently working on this task, and i was making it alot more complicated than it has to be.