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

Siddharth Pande
Siddharth Pande
9,045 Points

Feedback on my code

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

var students = [
  {
    name: "Siddharth",
    track: "Front-End Developer Course",
    achievements: "got badges",
    points: 4938,
  },

  {
    name: "Dave",
    track: "Golang",
    achievements: "Teacher at Treehouse",
    points: 14567,
  },

  {
    name: "Brendan",
    track: "Python for Beginners",
    achievements: "Moderator at Treehouse",
    points: 83000,
  },

  {
    name: "Anil",
    track: "HTML Basics",
    achievements: "Newbie",
    points: 25,
  },

  {
    name: "Saurabh",
    track: "Front-End Design",
    achievements: "Newbie",
    points: 56,
  },

];




function arrangeRecord(arr) {
  for (i = 0; i <= students.length; i += 1) {
    div.innerHTML += "<h4>Student: " + students[i].name + "</h4>" +
                     "<p>Track: " + students[i].track + "</p>" +
                     "<p>Points: " + students[i].points + "</p>" +
                     "<p>Achievements: " + students[i].achievements + "</p>                          </br>"; 
    }
}


  arrangeRecord(students);

1 Answer

Liam Clarke
MOD
Liam Clarke
Treehouse Moderator 19,274 Points

Hello

This is a good starting point for using data. We hold data in JSON quite a lot with JavaScript so doing the above is a good starting point.

There is a few things i would suggest if you wanted to make this a little more advanced as if it was a real scenario.

  • Your arrangeRecord function could be simplified to one line. Change your code to be for ... in / for ... of.
  • Put the students in a JSON file, see if you can figure out how to import it and readFile

Bonus

  • Add an Edit button and try work out how to writeFile

The above will be really good practice at handling JSON data which is used all the time, see if you can figure them out, Give me a message if you get stuck, ill give a bit more info.

Good Luck

Siddharth Pande
Siddharth Pande
9,045 Points

Thanks for your answer and I will work on the points that you mentioned here. If stuck I will contact you.