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 2 Solution

Michele Adami
Michele Adami
5,691 Points

This is my version. Pretty different but it works!

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

var input = '';
var questioni = [
  ['Come mi chiamo?', 'michele'],
  ['Quanto fa 2 + 2?', '4'],
  ['Call of duty รจ...', 'lezzo']
];
var check = [];


for(var i = 0; i < questioni.length; i++){
  input = prompt(questioni[i][0]);
  input = input.toLowerCase();
  if(input === questioni[i][1]){
   check[i] = '<p>' + (i+1) + '. Correct!</p>';
  } else{
    check[i] = '<p>' + (i+1) + '. Wrong! The correct answer is ' + questioni[i][1] + '</p>';
  }
}
print(check.join(''));
Gunhoo Yoon
Gunhoo Yoon
5,027 Points

Calling print function that uses document.write() one time is nice.

You can just do input = prompt(questioni[i][0]).toLowerCase().

questioni doesn't seem like a good variable name.

I don't think adding br tag is worth it since p tag already makes enough visual separation. Extra spacing can be easily done by editing CSS.

Also, mixing up string concatenation and addition can be pretty risky at times, even though you used parenthesis to avoid it. I'd prefer avoiding it at the first place.

Michele Adami
Michele Adami
5,691 Points

I used the print function because it was already present while i opened the workspace of the related video. Well, i used questioni as a variable name because in my first language is a familiar word. Yes, i noticed later that the br tag is useless because p is a block level element, in fact I change it later with an empty string. But, why mixing up strings and addition can be risky?

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

I actually said first one as being nice.

I get it if you are comfortable using your native language

Third one is just stylistic don't take it seriously.

So fourth one why is that risky?

If you have something like 'a' + 1 + 2 you will get 'a12'. Most of time JavaScript won't complain about this result so if this data is sent around your program it will lead to weird string or NaN value. It is not a problem as long as you know it for sure but mistakes can happen and it can be hard to find in large pool of codes.