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
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>`;
}
Ryan Soeder
Ryan Soeder
6,425 Points

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