Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Build an Object Challenge, Part 1

Caleb Jost
Caleb Jost
6,072 Points

Build an Object: My Solution

I couldn't believe it when this finally worked. Here's my solution, let me know if anyone has improvements. I still don't 100% understand that print(message) function.

var students = [ { Name: 'Caleb', Track: 'Front-end Development', Achievements: 50, Points: 1000, },

{ Name: 'Cristof', Track: 'FullStack JavaScript', Achievements: 49, Points: 999 },

{ Name: 'Yanet', Track: 'iOs Development', Achievements: 30, Points: 2000 },

{ Name: 'Boberton', Track: 'Front-end Development', Achievements: 78, Points: 3000 },

{ Name: 'Jimbo', Track: 'iOS Development', Achievements: 62, Points: 250 }, ];

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

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

print(html);

Here is my code, I know it's better to have it in a function, but this was a first try and it kind of awesomely worked :D

var html = document.getElementById('output');

for(var i = 0; i<students.length; i++){ for(var student in students[i]){ html.innerHTML += '<p>' + student + ': ' + students[i][student] + '</p>'; } }

The print message function is really cool actually, first with var outputDiv = document.getElementById('output') it lets you select an element from the DOM and then with outputDiv.innerHTML=message; it lets you modify it's content/structure/HTML by passing a parameter message which doesn't always have to be a structure with <p> or a <ul>, but anything you want.

I think you can improve your code definitely, so try this instead of the for you use: for(var i = 0; i<students.length; i++){ for(var student in students[i]){ html += '<p>' + student + ': ' + students[i][student] + '</p>'; } } P.S. You don't need commas on last array elements, last objects etc.

1 Answer

Joshua Petry
Joshua Petry
6,313 Points

Here's my code:

var printedRecords = "";

var student = [
  {name: 'Josh', track: 'Javascript', achievements: 10, points: 4560},
  {name: 'Jon', track: 'iOS', achievements: 10, points: 4560},
  {name: 'Sam', track: 'Java', achievements: 10, points: 4560},
  {name: 'Avery', track: 'PHP', achievements: 10, points: 4560},
  {name: 'Chelsea', track: 'Web Design', achievements: 10, points: 4560}
];

for ( i = 0; i < student.length; i++ ) {
  printedRecords += '<p>Name: ' + student[i].name + '<br>';
  printedRecords += 'Track: ' + student[i].track + '<br>';
  printedRecords += 'Achievements: ' + parseInt(student[i].achievements) + '<br>';
  printedRecords += 'Points: ' + parseInt(student[i].points) + '</p>';
}

document.write(printedRecords);
Junet Ismail
Junet Ismail
3,563 Points

Hi Josh you could refactor the second part of your code as such using the for in as covered in the course

var title =['Name','Track','Achievements','Points']; var titlecount ;

for ( i = 0; i < student.length; i++ ) { titlecount = 0; for (var identifier in student){

printedRecords += '<p> ' +title[titlecount]+':'+ student[i][identifier] + '</p>'; titlecount+=1; } } } document.write(printedRecords);