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

Bernard Bond
Bernard Bond
3,302 Points

This is how I answered the solution. Any ways to improve this code?

var students = [ { name: 'Dave', track: 'Front End Development', achievements: 158, points: 14730 }, { name: 'Jody', track: 'iOS Development with Swift', achievements: '175', points: '16375' }, { name: 'Jordan', track: 'PHP Development', achievements: '55', points: '2025' }, { name: 'John', track: 'Learn WordPress', achievements: '40', points: '1950' }, { name: 'Trish', track: 'Rails Development', achievements: '5', points: '350' } ];

var message = ''; function print(message){ var div = document.getElementById('output'); div.innerHTML = message; return message; }

for (var i = 0; i < students.length; i++) { message += '<p><br><strong>Student</strong>: ' + students[i].name + '</p></br>'; message += '<p>Track: ' + students[i].track + '</p>'; message += '<p>Achievement: ' + students[i].achievements + '</p>'; message += '<p>Points: ' + students[i].points; + '</p>'; }

print(message);

Steven Parker
Steven Parker
231,248 Points

A big improvement would be to display it with proper formatting. :laughing:

Use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:

3 Answers

Snehith Reddy
Snehith Reddy
1,301 Points
var student = [
 {student :'Sarah', track : 'IOS', achievement : '500', points :'1500'},
 {student :'Mark', track : 'Android', achievement : '200', points :'1200'},
 {student :'Russel', track : 'Java', achievement : '300', points :'1300'},
 {student :'Lindsey', track : '.Net', achievement : '600', points :'1600'},
 {student :'Carmen', track : 'ASP', achievement : '100', points :'1100'}
]

for (i=0; i < student.length ; i++){
  document.write('<b>Student: ' + student[i].student + '</b>');
  document.write('<br>'+ 'Track: ' + student[i].track);
  document.write('<br>'+ 'Achievement: '+ student[i].achievement);
  document.write('<br>'+ 'Track: ' + student[i].points);
  document.write('<br><br>');
}

Looks primitive, but this is how i did it and i'm pretty sure that's how the OP did it too.

Jerry Wu
Jerry Wu
1,845 Points

This is what I did. Which is almost identical to what you have. However, I feel like storing the function in a variable is redundant so I just left the function alone, declared a variable HTML (like the previous lessons), and then had the loop += to the HTML variable.

function print(message){
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}

var HTML = '';

var students = [
  {
   Name: 'Jerry',
   Track: 'Front End Development',
   Achievements: 10,
   Points: 1216
  },
  {
   Name: 'Angelica',
   Track: 'iOS',
   Achievements: 8,
   Points: 1000
  },
  {
   Name: 'Robert',
   Track: 'Full Stack Developer',
   Achievements: 47,
   Points: 17000
  },
  {
   Name: 'Jordan',
   Track: 'Front End Developer',
   Achievements: 7,
   Points: 900
  },
  {
   Name: 'Kai',
   Track: 'Full Stack Developer',
   Achievements: 36,
   Points: 14000
  }
];

for (var i = 0; i < students.length; i++){
  HTML += '<br>'
  HTML += '<b>Student: ' + students[i].Name + '</b><br>';
  HTML += 'Track: ' + students[i].Track + '<br>';
  HTML += 'Achievements: ' + students[i].Achievements + '<br>';
  HTML += 'Points: ' + students[i].Points + '<br>';
}

print(HTML);
Steven Parker
Steven Parker
231,248 Points

You could use template interpolation.

I'm not sure if it actually qualifies as "improvement", but:

for (let i=0; i < student.length ; i++){
  let s = student[i];
  document.write(`<b>Student: ${s.student}</b>`);
  document.write(`<br>Track: ${s.track}`);
  document.write(`<br>Achievement: ${s.achievement}`);
  document.write(`<br>Track: ${s.points}`);
  document.write('<br><br>');
}