console.log("whats up everyone");
var msg = "whats up everyone!";
console.log(msg);
function logIt(output) {
console.log(output);
}
logIt(msg);
console.log("Reuse of logIT")
logIt("Hello, Students!");
logIt(2022)
function logItType(output) {
console.log(typeof output, ";", output);
}
console.log("Looking at dynamic nature of types in JavaScript")
logItType("hello"); // String
logItType(2022); // Number
logItType([1, 2, 3]); // Object is generic for this Array, which similar to Python List
function PrintType(output) { // function to output data type and data into terminal
console.log(typeof output, ", ", output);
}
// define a function to hold data for a Person
function Person(name, Grad, residence) {
this.name = name;
this.Grad = Grad;
this.residence = residence;
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, age: this.age, residence: this.residence, role: this.role};
const json = JSON.stringify(obj);
return json;
}
// make a new Person and assign to variable student
var student = new Person("elijah", 2024, "SanDiego"); // object type is easy to work with in JavaScript
PrintType(student); // before role
PrintType(student.toJSON());
// output of Object and JSON/string associated with student
student.setRole("Student"); // set the role
PrintType(student);
PrintType(student.toJSON());
// define a student Array of Person(s)
var students = [
new Person("Elijah ", 2024, "San Diego"),
new Person("Josh", 2024, "San Diego"),
new Person("Chris", 2024, "San Diego"),
new Person("Jeffery", 2023, "San Diego")
];
var teacher = new Person("Mr. M", "?", "San Diego")
// 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
PrintType(compsci.classroom); // constructed classroom object
PrintType(compsci.classroom[0].name); // abstract 1st objects name
PrintType(compsci.json[0]); // show json conversion of 1st object to string
PrintType(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>" + "Grad" + "</mark></th>";
body += "<th><mark>" + "Residence" + "</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.Grad + "</td>";
body += "<td>" + row.residence + "</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());