Java Script
function logIt(output) {
console.log(output);
}
function logItType(output) {
console.log(typeof output, ";", output);
}
// define a function to hold data for a Person
function Person(name, ghID, classOf, School) {
this.name = name;
this.ghID = ghID;
this.classOf = classOf;
this.role = "";
this.School = School;
}
// define a setter for role in Person data
Person.prototype.setRole = function(role) {
this.role = role;
}
Person.prototype.setSchool = function(School){
this.School = School;
}
// define a JSON conversion "method" associated with Person
Person.prototype.toJSON = function() {
const obj = {name: this.name, ghID: this.ghID, classOf: this.classOf, 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); // object type is easy to work with in JavaScript
teacher.setRole("teacher")
logItType(teacher); // before role
logItType(teacher.toJSON()); // ok to do this even though role is not yet defined
// Add myself as a new person
var caleb = new Person("Caleb", "CubNavarro", 2024);
caleb.setRole("Coolguy")
caleb.setSchool("Del Norte")
logItType(caleb);
// output of Object and JSON/string associated with Teacher
teacher.setRole("Teacher"); // set the role
teacher.setSchool("Del Norte")
logItType(teacher);
logItType(teacher.toJSON());
var students = [
new Person("Toby", "Tobyleeder", 2024, "Del Norte"),
new Person("Nathan", "Nathan-Capule", 2024, "Del Norte"),
new Person("Gene", "Gene", 2024, "Del Norte"),
new Person("Theo", "Theothegreat", 2024, "Del Norte"),
new Person("Caden", "cadenthebot", 2024, "Del Norte"),
];
// define a classroom and build Classroom objects and json
function Classroom(teacher, students){ // 1 teacher, many student
// start Classroom with Teacher
teacher.setRole("Teacher");
teacher.setSchool("Del Norte")
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
// 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>" + "GitHub ID" + "</mark></th>";
body += "<th><mark>" + "Class Of" + "</mark></th>";
body += "<th><mark>" + "Role" + "</mark></th>";
body += "<th><mark>" + "School" + "</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.role + "</td>";
body += "<td>" + row.School + "</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());