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

Need some ideas for shortening my script

Here what I've done so far. Is there any suggestion that make my script a little bit briefer than this?

var questions = [
  ['How many states are there in the United State?',50],
  ['How many legs does human have?',2],
  ['What planet are you living in?','earth']
];
var correctAnswer;
var count = 0;

function the1stList() {
  var questionaire;
  var list1 = '<ol> You got these questions correct:';
  var list2 = '<ol> You got these questions incorrect:';
  for (var i = 0; i < questions.length; i += 1) {
    questionaire = prompt(questions[i][0]);
    if (isNaN(questionaire) === true) {
      questionaire = questionaire.toLowerCase();
      if (questionaire === questions[i][1]) {
        correctAnswer = true;
      } else {
        correctAnswer = false;
      }
    } else {
      questionaire = parseInt(questionaire);
      if (questionaire === questions[i][1]) {
        correctAnswer = true;
      } else {
        correctAnswer = false;
      }
    }
    if (correctAnswer === true) {
      count += 1;
      list1 += '<li>'+ questions[i][0] +'</li>';
    } else {
      list2 += '<li>'+ questions[i][0] +'</li>';
    }
    }
  var list = '<p><strong> You got '+ count +' questions right.</strong></p>';
  list1 += '</ol>';
  list2 += '</ol>';

  print(list);
  print(list1);
  print(list2);

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

Many thanks!

Amrit Pandey
Amrit Pandey
17,595 Points

I can't see your code, could you paste it here.

Done, sorry, I didn't know how to create a code block.

Amrit Pandey
Amrit Pandey
17,595 Points

What do you mean by brief? Is it refactoring? or do you mean if there should be comments or not?

One, thing that I saw that you were using numbers in between as function name like the1stList using numbers for naming is often confusing. Rest is fine!

Well, I mean refactoring by the way. As you can see, my script takes a lot of lines especially in the part using conditional statement if.

1 Answer

Amrit Pandey
Amrit Pandey
17,595 Points

Well, looking at the code, there are lots of if and else, but it nothing much can be done actually. There are few things I can suggest, but you'll understand them later as you practice.

One of them are:

    if (correctAnswer) {
      count += 1;
      list1 += '<li>'+ questions[i][0] +'</li>';
    }

I did not compared correctAnswer to true, because if it is true then the code will be executed. There are very minor changes you can do like short-circuting, using ternary operator to decide if/else but I suggest you find and study by yourself, It'll be a great practice!