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 2

My solution (trying to use modern JS)

let html = "";

const students = [{
    name: "John",
    track: "iOS",
    achievements: 111,
    points: 102124342
  },
  {
    name: "Mary",
    track: "Web Design",
    achievements: 222,
    points: 20353536363
  },
  {
    name: "Richard",
    track: "Android",
    achievements: 555,
    points: 543243243240
  },
  {
    name: "Elisa",
    track: "Full Stack",
    achievements: 777,
    points: 902343242
  },
  {
    name: "Alice",
    track: "Front End",
    achievements: 333,
    points: 8232424424240
  }
];


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

for (let x in students) {
  html += `<h2>Student: ${students[x].name}</h2>
 <ul>
  <li>Track: ${students[x].track}</li>
  <li>Points: ${students[x].points}</li>
  <li>Achievements: ${students[x].achievements}</li>
  </ul>`;
}

print(html);

1 Answer

Jamie Reardon
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jamie Reardon
Treehouse Project Reviewer

Nice stuff! You could even go further with the newer syntax by converting your function to an arrow function, like so:

const print = message => {
  let outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}

The only other thing (being nit-picky as I am not sure if you are doing this to improve the look of the code more?) is that you could also clean up the template literal string by indenting the code like so:

for (let x in students) {
  html += `
    <h2>Student: ${students[x].name}</h2>
    <ul>
      <li>Track: ${students[x].track}</li>
      <li>Points: ${students[x].points}</li>
      <li>Achievements: ${students[x].achievements}</li>
    </ul>`;
}

This is a beautiful solution but will a for...in loop work on "students" given that it's an array and not an object?