console.log("Hi");
Hi
var msg = "Hi";
console.log(msg);
Hi
function logIt(output) {
    console.log(output);
}
logIt(msg);
Hi
console.log("Reuse of logIT")
logIt("Hi");
logIt(2022)
Reuse of logIT
Hi
2022
function logItType(output) {
    console.log(typeof output, ";", output);
}
console.log("Functions in JavaScript")
logItType("hi"); // String
logItType(2020);    // Number
logItType([1, 2, 3]);  // Object is generic for this Array, which similar to Python List
Functions in JavaScript
string ; hi
number ; 2020
object ; [ 1, 2, 3 ]
// define a function to hold data for a Person
function Person(name, sport, age) {
  this.name = name;
  this.sport = sport;
  this.age = age;
  this.role = "";
}
// define a setter for role in Person data
Person.prototype.setRole = function(role) {
  this.role = role;
}
// define a JSON conversion "method" associated with Person
Person.prototype.toJSON = function() {
  const obj = {name: this.name, sport: this.sport, age: this.age, role: this.role};
  const json = JSON.stringify(obj);
  return json;
}
// make a new Person and assign to variable teacher
var teacher = new Person("Ms Smith", "tennis", 32);  // object type is easy to work with in JavaScript
logItType(teacher);  // before role
logItType(teacher.toJSON());  // ok to do this even though role is not yet defined
// output of Object and JSON/string associated with Teacher
teacher.setRole("Teacher");   // set the role
logItType(teacher);
logItType(teacher.toJSON());
object ; Person { name: 'Ms Smith', sport: 'tennis', age: 32, role: '' }
string ; {"name":"Ms Smith","sport":"tennis","age":32,"role":""}
object ; Person { name: 'Ms Smith', sport: 'tennis', age: 32, role: 'Teacher' }
string ; {"name":"Ms Smith","sport":"tennis","age":32,"role":"Teacher"}
// define a student Array of Person(s)
var students = [
    new Person("Tanisha", "hockey", 15),
    new Person("Claire", "hockey", 15),
    new Person("Amitha", "track", 16),
    new Person("Grace", "basketball", 17),
];
// define a classroom and build Classroom objects and json
function Classroom(teacher, students){ // 1 teacher, many student
    // start Classroom with Teacher
    teacher.setRole("Teacher");
    this.teacher = teacher;
    this.classroom = [teacher];
    // add each Student to Classroom
    this.students = students;
    this.students.forEach(student => { student.setRole("Student"); this.classroom.push(student); });
    // build json/string format of Classroom
    this.json = [];
    this.classroom.forEach(person => this.json.push(person.toJSON()));
}
// make a CompSci classroom from formerly defined teacher and students
compsci = new Classroom(teacher, students);
// output of Objects and JSON in CompSci classroom
logItType(compsci.classroom);  // constructed classroom object
logItType(compsci.classroom[0].name);  // abstract 1st objects name
logItType(compsci.json[0]);  // show json conversion of 1st object to string
logItType(JSON.parse(compsci.json[0]));  // show JSON.parse inverse of JSON.stringify
object ; [ Person { name: 'Ms Smith', sport: 'tennis', age: 32, role: 'Teacher' },
  Person { name: 'Tanisha', sport: 'hockey', age: 15, role: 'Student' },
  Person { name: 'Claire', sport: 'hockey', age: 15, role: 'Student' },
  Person { name: 'Amitha', sport: 'track', age: 16, role: 'Student' },
  Person { name: 'Grace', sport: 'basketball', age: 17, role: 'Student' } ]
string ; Ms Smith
string ; {"name":"Ms Smith","sport":"tennis","age":32,"role":"Teacher"}
object ; { name: 'Ms Smith', sport: 'tennis', age: 32, role: 'Teacher' }
// define an HTML conversion "method" associated with Classroom
Classroom.prototype._toHtml = function() {
    // HTML Style is build using inline structure
    var style = (
      "display:inline-block;" +
      "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>" + "Sport" + "</mark></th>";
    body += "<th><mark>" + "Age" + "</mark></th>";
    body += "<th><mark>" + "Role" + "</mark></th>";
    body += "</tr>";
    // Data of Array, iterate through each row of compsci.classroom
    for (var row of compsci.classroom) {
      // tr for each row, a new line
      body += "<tr>";
      // td for each column of data
      body += "<td>" + row.name + "</td>";
      body += "<td>" + row.sport + "</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(compsci._toHtml());
</table></div> </div> </div> </div> </div> </div> </div>
NameSportAgeRole
Ms Smithtennis32Teacher
Tanishahockey15Student
Clairehockey15Student
Amithatrack16Student
Gracebasketball17Student