Build a Person Function/Class object and JSON

// define function logItType
function logItType(output) {
    console.log(typeof output, ";", output); //will print out the type of data, semicolon, and the data itself
}

// define a function to hold data for a Person
function Person(name, ghID, age) {
    this.name = name;
    this.ghID = ghID;
    this.age = age;
    this.role = "";
}

// define a setter for role in Person data
Person.prototype.setRole = function(role) { //Definition of a prototype allow for the definition of a method associated with the function<
    this.role = role;
}

// define a JSON conversion "method" associated with Person
Person.prototype.toJSON = function() {
    const obj = {name: this.name, ghID: this.ghID, age: this.age, role: this.role};
    const json = JSON.stringify(obj);  // json/string is useful when passing data on internet
    return json;
}

// make a new Person and assign to variable student
var student = new Person("Linda", "LindaLiu1202", 15);  // object type is easy to work with in JavaScript
logItType(student);  // before role
logItType(student.toJSON());  // ok to do this even though role is not yet defined

// output of Object and JSON/string associated with Student
student.setRole("Student");   // set the role
logItType(student); 
logItType(student.toJSON());
object ; Person { name: 'Linda', ghID: 'LindaLiu1202', age: 15, role: '' }
string ; {"name":"Linda","ghID":"LindaLiu1202","age":15,"role":""}
object ; Person { name: 'Linda', ghID: 'LindaLiu1202', age: 15, role: 'Student' }
string ; {"name":"Linda","ghID":"LindaLiu1202","age":15,"role":"Student"}

Build a Website Array/List of Persons and JSON

function logItType(output) {
    console.log(typeof output, ";", output); //will print out the type of data, semicolon, and the data itself
}


function Person(name, number, age) {
    this.name = name;
    this.number = number;
    this.age = age;
    this.role = "";
}

Person.prototype.setRole = function(role) { //Definition of a prototype allow for the definition of a method associated with the function<
    this.role = role;
}


Person.prototype.toJSON = function() {
    const obj = {name: this.name, number: this.number, age: this.age, role: this.role};
    const json = JSON.stringify(obj);  // json/string is useful when passing data on internet
    return json;
}

var coder = new Person("Linda", 8581234567, 15);  // object type is easy to work with in JavaScript
logItType(coder);  // before role
logItType(coder.toJSON());  // ok to do this even though role is not yet defined


coder.setRole("Coder");   // set the role
logItType(coder); 
logItType(coder.toJSON());
object ; Person { name: 'Linda', number: 8581234567, age: 15, role: '' }
string ; {"name":"Linda","number":8581234567,"age":15,"role":""}
object ; Person { name: 'Linda', number: 8581234567, age: 15, role: 'Coder' }
string ; {"name":"Linda","number":8581234567,"age":15,"role":"Coder"}
// define a user Array of Person(s)
var users = [ 
    new Person("Emily", "8588888888", 17),
    new Person("Alice", "8583947583", 19),
    new Person("Chloe", "8583948501", 18),
    new Person("Ann", "8583749284", 16),
    new Person("Abby", "8583749843", 15)
];

// define a website and build Website objects and json
function Website(coder, users){ // 1 teacher, many student
    // start website with Coder

    this.coder = coder;
    this.website = [coder];
    // add each User to Website
    this.users = users;
    this.users.forEach(user => { user.setRole("User"); this.website.push(user); });
    // build json/string format of Website
    this.json = [];
    this.website.forEach(person => this.json.push(person.toJSON()));
}

// make a travel webiste from formerly defined coder and users
travel = new Website(coder, users);

// output of Objects and JSON in Travel website
logItType(travel.website);  // constructed website object
logItType(travel.website[0].name);  // abstract 1st objects name
logItType(travel.json[0]);  // show json conversion of 1st object to string
logItType(JSON.parse(travel.json[0]));  // show JSON.parse inverse of JSON.stringify
object ; [ Person { name: 'Linda', number: 8581234567, age: 15, role: 'Coder' },
  Person { name: 'Emily', number: '8588888888', age: 17, role: 'User' },
  Person { name: 'Alice', number: '8583947583', age: 19, role: 'User' },
  Person { name: 'Chloe', number: '8583948501', age: 18, role: 'User' },
  Person { name: 'Ann', number: '8583749284', age: 16, role: 'User' },
  Person { name: 'Abby', number: '8583749843', age: 15, role: 'User' } ]
string ; Linda
string ; {"name":"Linda","number":8581234567,"age":15,"role":"Coder"}
object ; { name: 'Linda', number: 8581234567, age: 15, role: 'Coder' }

IJavaScript and Table formatting using toHTML method

// define an HTML conversion "method" associated with Webiste
Website.prototype._toHtml = function() {
    // HTML Style is build using inline structure
    var style = (
      "display:inline-block;" +
      "border: 2px solid white;" +
      "box-shadow: 0.4em 0.4em 0.4em white;"
    );
  
    // 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>" + "Number" + "</mark></th>";
    body += "<th><mark>" + "Age" + "</mark></th>";
    body += "<th><mark>" + "Role" + "</mark></th>";
    body += "</tr>";
    // Data of Array, iterate through each row of travel.website 
    for (var row in travel.website) {
      // tr for each row, a new line
      body += "<tr>";
      // td for each column of data
      body += "<td>" + travel.website[row].name + "</td>";
      body += "<td>" + travel.website[row].number + "</td>";
      body += "<td>" + travel.website[row].age + "</td>";
      body += "<td>" + travel.website[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(travel._toHtml());
</table></div> </div> </div> </div> </div> </div>

Team Project (travel website) Itinerary Table

function logItType(output) {
  console.log(typeof output, ";", output); //will print out the type of data, semicolon, and the data itself
}

function Time(start, destination, end) {
  this.start = start;
  this.destination = destination;
  this.end = end;
  this.month = "";
}

Time.prototype.setMonth = function(month) { //Definition of a prototype allow for the definition of a method associated with the function<
  this.month = month;
}


Time.prototype.toJSON = function() {
  const obj = {start: this.start, destination: this.destination, end: this.end, role: this.role};
  const json = JSON.stringify(obj);  // json/string is useful when passing data on internet
  return json;
}

var august = new Time("8/20", "Los Angeles", "8/22");  // object type is easy to work with in JavaScript

august.setMonth("August");   // set the role


// define a user Array of Person(s)
var septembers = [ 
  new Time("9/5", "China", "9/30"),
  new Time("9/17", "Hawii", "9/20"),
  new Time("9/21", "Japan", "9/27"),
  new Time("9/23", "Korea", "9/29"),
  new Time("9/25", "New York", "9/28")
];

// define a website and build Website objects and json
function Itinerary(august, septembers){ // 1 teacher, many student
  // start website with Coder
  august.setMonth("August");
  this.august = august;
  this.itinerary = [august];
  // add each User to Website
  this.septembers = septembers;
  this.septembers.forEach(september => { september.setMonth("September"); this.itinerary.push(september); });
  // build json/string format of Website
  this.json = [];
  this.itinerary.forEach(time => this.json.push(time.toJSON()));
}

// make a travel webiste from formerly defined coder and users
travel = new Itinerary(august, septembers);


// define an HTML conversion "method" associated with Webiste
Itinerary.prototype._toHtml = function() {
    // HTML Style is build using inline structure
    var style = (
      "display:inline-block;" +
      "border: 2px solid white;" +
      "background-color: white;" +
      "color: black;"
    );
  
    console.log("Itinerary Example")

    // HTML Body of Table is build as a series of concatenations (+=)
    var body = "";
    // Heading for Array Columns
    body += "<tr>";
    body += "<th><mark>" + "Start Date" + "</mark></th>";
    body += "<th><mark>" + "Destination" + "</mark></th>";
    body += "<th><mark>" + "End Date" + "</mark></th>";
    body += "<th><mark>" + "Month" + "</mark></th>";
    body += "</tr>";
    // Data of Array, iterate through each row of travel.itinerary 
    for (var row in travel.itinerary) {
      // tr for each row, a new line
      body += "<tr>";
      // td for each column of data
      body += "<td>" + travel.itinerary[row].start + "</td>";
      body += "<td>" + travel.itinerary[row].destination + "</td>";
      body += "<td>" + travel.itinerary[row].end + "</td>";
      body += "<td>" + travel.itinerary[row].month + "</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(travel._toHtml());
Itinerary Example
Name Number Age Role
Linda 8581234567 15 Coder
Emily 8588888888 17 User
Alice 8583947583 19 User
Chloe 8583948501 18 User
Ann 8583749284 16 User
Abby 8583749843 15 User
</table></div> </div> </div> </div> </div> </div> </div>
Start Date Destination End Date Month
8/20 Los Angeles 8/22 August
9/5 China 9/30 September
9/17 Hawii 9/20 September
9/21 Japan 9/27 September
9/23 Korea 9/29 September
9/25 New York 9/28 September