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 Data Using Objects The Build an Object Challenge, Part 2 Solution

Mar Bocatcat
Mar Bocatcat
7,405 Points

Did this challenge different(Review Code Please!)

var students = [

      {
      name: 'Mar Bocatcat', tracks: 'JavaScript', achievements: 20, points: 5521
      },
      {
      name: 'Darlene Cerna', tracks: 'Html/css', achievements: 24, points: 5342
      },
      {
       name: 'Michael Scott', tracks: 'Python', achievements: 84, points: 3412
      },
      {
     name: 'Pam Beasly', tracks: 'Java', achievements: 21, points: 1234
      },

     ];

     function printHtml(message) {
      document.write('<p>' + 'Track: ' + message +  '</p>');
      }

    function printachiev(message) {
     document.write('<p>' + 'Achievements: ' + message +  '</p>');
     }

     function printpoints(message) {
      document.write('<p>' + 'Points: ' + message +  '</p>');
      }


     function printH(message) {
      document.write('<h2>' + message + '</h2>');
     }




    for ( var prop in students ) {

    printH( students[prop].name);
    printHtml(students[prop].tracks);
    printachiev( students[prop].achievements);
    printpoints(students[prop].points);
    }

1 Answer

vanessa
vanessa
3,455 Points

Please try to practice "DRY" (don't repeat yourself) programming

Too many functions doing same thing. I think it would be better if you save all the output in one variable first. And Print it out once.

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

  var html;
  for ( var prop in students ) {
    html = '<p> Name : ' + students[prop].name + '</p>';
    html+= '<p> Tracks : ' + students[prop].tracks + '</p>';
    html+= '<p> Achievements : ' + students[prop].achievements + '</p>';
    html+= '<p> Students : ' + students[prop].points + '</p><br>';
    printHtml(html);
  }