// defines a function that outputs to the console the message
function logt(output) {
    console.log(output);
}
// calls to the function 
logt("hi");

HTML fragments along with Javascript table

  • outputs the lakers roster along with the coach using javascript and html that uses json to convert the objects into strings to output into the table!
// define a function to hold data for a Person
function Person(name, height, age) {
    this.name = name;
    this.height = height;
    this.age = age;
    this.role = "";
}
// Sets the role of the person
Person.prototype.setRole = function(role) {
    this.role = role;
}
//Defines Coach
var coach = new Person("Darwin Ham", "6'7", 48);
coach.setRole("Coach");


// JSON conversion that changes an object into a string
Person.prototype.toJSON = function() {
    const obj = {name: this.name, height: this.height, age: this.age, role: this.role};
    const json = JSON.stringify(obj);
    return json;
}
// data about players
var players = [ 
    new Person("Carmelo Anthony", "6'7", 38),
    new Person("Lebron James", "6'9", 37),
    new Person("Russel Westbrook", "6'3", 33),
    new Person("Dennis Shroder", "6'1", 29),
    new Person("Anthony Davis", "6'10", 29),
    new Person("Dwight Howard", "6'10", 36)
];

function Team(coach, players){ // 1 coach with a group of players to make a team
    // starts the list with the coach
    this.coach = coach;
    this.team = [coach];
    // adding players to the array that we just made
    this.players = players;
    this.players.forEach(player => { player.setRole("Player"); this.team.push(player); });
    // creates the json list of string by calling back to the function earlier
    this.json = [];
    this.team.forEach(player => this.json.push(player.toJSON()));
}
//defining the team
var lakers = new Team(coach, players);
// define an HTML conversion "method" associated with Classroom
Team.prototype._toHtml = function() {
    // HTML Style is build using inline structure
    var style = (
      "display:inline-block;" +
      "background:black;" +
      "border: 2px solid grey;" +
      "box-shadow: 0.8em 0.4em 0.4em grey;"
    );
  
    // HTML Body of Table is build as a series of concatenations (+=)
    var body = "";
    // Heading for Array Columns
    body += "<tr>";
    body += "<th><mark>" + "Name" + "</mark></th>";
    body += "<th><mark>" + "Height" + "</mark></th>";
    body += "<th><mark>" + "Age" + "</mark></th>";
    body += "<th><mark>" + "Role" + "</mark></th>";
    body += "</tr>";
    // Data of Array, iterate through each row of lakers team
    for (var row of lakers.team) {
      // tr for each row, a new line
      body += "<tr>";
      // td for each column of data
      body += "<td>" + row.name + "</td>";
      body += "<td>" + row.height + "</td>";
      body += "<td>" + row.age + "</td>";
      body += "<td>" + row.role + "</td>";
      // tr to end line
      body += "<tr>";
    }
  
     // Build and HTML fragment of div, table, table body
    return (
      "<div style='" + style + "'>" +
        "<table>" +
          body +
        "</table>" +
      "</div>"
    );
  
  };
  
  // IJavaScript HTML processor receive parameter of defined HTML fragment
  $$.html(lakers._toHtml());