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 1

Peter Ajuzie
seal-mask
.a{fill-rule:evenodd;}techdegree
Peter Ajuzie
Full Stack JavaScript Techdegree Student 2,719 Points

Help with housekeeping (line breaks)

Please I need some help with adding line breaks after each student object when it prints to the screen.

          <p>
var students = [
  {Name: 'Peter', Track: 'Front End Development', Achievements: 22, Points: 1545},
  {Name: 'Paul', Track: 'iOS', Achievements: 19, Points: 1405},
  {Name: 'Jude', Track: 'Web Design', Achievements: 10, Points: 890},
  {Name: 'Lola', Track: 'Python', Achievements: 13, Points: 1032},
  {Name: 'Tola', Track: 'Android', Achievements: 15, Points: 1198}
]

// ENCAPSULATE THE LOOPS IN A FUNCTION
function studentList( students ) {
  for (var i = 0; i < students.length; i++) {
    for (var prop in students[i]) {
    student =  document.write('<p>'+ prop + ': ' + students[i][prop] + '</p>');
    }
  }
}

// Call the function and print the list to the screen
studentList(students);
</p>
          ```

1 Answer

Steven Parker
Steven Parker
231,236 Points

By enclosing your outputs in paragraph tags you should get not only line breaks but some margin space between the lines. If you wanted only line breaks you could use <br> instead of paragraphs:

      document.write(prop + ': ' + students[i][prop] + '<br>');

And if you wanted an extra line in between students, you could write that in the outer loop:

  for (var i = 0; i < students.length; i++) {
    for (var prop in students[i]) {
      document.write(prop + ': ' + students[i][prop] + '<br>');
    }
    document.write('<br>');  // extra line between students
  }

Also, your script code seems to have some stray HTML (paragraph tags) at the beginning and end.

Peter Ajuzie
seal-mask
.a{fill-rule:evenodd;}techdegree
Peter Ajuzie
Full Stack JavaScript Techdegree Student 2,719 Points

Thanks, Steven!

Per the stray html paragraph tags, I was still learning the markdown method for codes here lol. So forgot to delete them from the example html code from the cheatsheet.