console.log("Mr. Mortensen is cool!"); // console.log displays the parameter (hello word) in the console
Mr. Mortensen is cool!
var msg = "Mr. Mortensen is cool"; // This is defining the message as a variable to be used in the console.log funciton
console.log(msg);
Mr. Mortensen is cool
function logIt(output) { // this is defining a function, named logIt
    console.log(output);
}
logIt(msg);
Mr. Mortensen is cool
console.log("Reuse of logIT") 
logIt("Hello, Students!"); // calls a string
logIt(2022) // calls a number
Reuse of logIT
Hello, Students!
2022
function logItType(output) {
    console.log(typeof output, ";", output); //typeof= type of object
}
console.log("Looking at dynamic nature of types in JavaScript")
logItType("hello"); // String
logItType(2020);    // Number
logItType([1, 2, 3]);  // Object is generic for this Array, which similar to Python List
Looking at dynamic nature of types in JavaScript
string ; hello
number ; 2020
object ; [ 1, 2, 3 ]
// define a function to hold data for a Person
function Person(name, ghID, classOf, haircolor, favfood) {
    this.name = name;
    this.ghID = ghID;
    this.classOf = classOf;
    this.haircolor= haircolor;
    this.favfood= favfood;
    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, ghID: this.ghID, classOf: this.classOf, haircolor: this.haircolor, favfood: this.favfood, role: this.role};
    const json = JSON.stringify(obj);
    return json;
}

// make a new Person and assign to variable teacher
var teacher = new Person("Mr M", "jm1021", 1977, "blue", "pasta");
teacher.setRole("Teacher");

// output of Object and JSON/string associated with Teacher
logItType(teacher);  // object type is easy to work with in JavaScript
logItType(teacher.toJSON());  // json/string is useful when passing data on internet
object ; Person {
  name: 'Mr M',
  ghID: 'jm1021',
  classOf: 1977,
  haircolor: 'blue',
  favfood: 'pasta',
  role: 'Teacher' }
string ; {"name":"Mr M","ghID":"jm1021","classOf":1977,"haircolor":"blue","favfood":"pasta","role":"Teacher"}
// define a student Array of Person(s)
var students = [ 
    new Person("Anthony", "tonyhieu", 2022, "pink", "pizza"),
    new Person("Bria", "B-G101", 2023, "blonde", "dirt"),
    new Person("Allie", "xiaoa0", 2023, "ginger", "apples"),
    new Person("Tigran", "Tigran7", 2023, "black", "chocolate"),
    new Person("Rebecca", "Rebecca-123", 2023, "grey", "sushi"),
    new Person("Vidhi", "unknown", 2024, "green", "urmom")
];

// 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: 'Mr M',
    ghID: 'jm1021',
    classOf: 1977,
    haircolor: 'blue',
    favfood: 'pasta',
    role: 'Teacher' },
  Person {
    name: 'Anthony',
    ghID: 'tonyhieu',
    classOf: 2022,
    haircolor: 'pink',
    favfood: 'pizza',
    role: 'Student' },
  Person {
    name: 'Bria',
    ghID: 'B-G101',
    classOf: 2023,
    haircolor: 'blonde',
    favfood: 'dirt',
    role: 'Student' },
  Person {
    name: 'Allie',
    ghID: 'xiaoa0',
    classOf: 2023,
    haircolor: 'ginger',
    favfood: 'apples',
    role: 'Student' },
  Person {
    name: 'Tigran',
    ghID: 'Tigran7',
    classOf: 2023,
    haircolor: 'black',
    favfood: 'chocolate',
    role: 'Student' },
  Person {
    name: 'Rebecca',
    ghID: 'Rebecca-123',
    classOf: 2023,
    haircolor: 'grey',
    favfood: 'sushi',
    role: 'Student' },
  Person {
    name: 'Vidhi',
    ghID: 'unknown',
    classOf: 2024,
    haircolor: 'green',
    favfood: 'urmom',
    role: 'Student' } ]
string ; Mr M
string ; {"name":"Mr M","ghID":"jm1021","classOf":1977,"haircolor":"blue","favfood":"pasta","role":"Teacher"}
object ; { name: 'Mr M',
  ghID: 'jm1021',
  classOf: 1977,
  haircolor: 'blue',
  favfood: 'pasta',
  role: 'Teacher' }

IJavaScript and Table formatting using toHTML method

  • The 3-part JavaScript in the _toHTML method
    • Style part is building CSS inline formatting
    • Body part is constructing the Table Rows (tr), Table Headings (th), and Table Data (td). The table data is obtained from a Classroom object. The JavaScript for loop allows the construction of a new row of data for each person object in the Array.
    • Return part creates the HTML fragment for rendering
// define an HTML conversion "method" associated with Classroom
Classroom.prototype._toHtml = function() {
  // HTML Style is build using inline structure
  var style = (
    "display:inline-block;" +
    "background:#7E006C;" +
    "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>" + "GitHub ID" + "</mark></th>";
  body += "<th><mark>" + "Class Of" + "</mark></th>";
  body += "<th><mark>" + "Hair Color" + "</mark></th>";
  body += "<th><mark>" + "Favorite Food" + "</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.ghID + "</td>";
    body += "<td>" + row.classOf + "</td>";
    body += "<td>" + row.haircolor + "</td>";
    body += "<td>" + row.favfood + "</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>
Name GitHub ID Class Of Hair Color Favorite Food Role
Mr M jm1021 1977 blue pasta Teacher
Anthony tonyhieu 2022 pink pizza Student
Bria B-G101 2023 blonde dirt Student
Allie xiaoa0 2023 ginger apples Student
Tigran Tigran7 2023 black chocolate Student
Rebecca Rebecca-123 2023 grey sushi Student
Vidhi unknown 2024 green urmom Student