Javascript Tutorial
Javascript!
console.log("Hello!");
var msg = "Hello, World!";
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");
logItType(2020);
logItType([1, 2, 3]);
var students = [
new Person("Anthony", "tonyhieu", 2022),
new Person("Bria", "B-G101", 2023),
new Person("Allie", "xiaoa0", 2023),
new Person("Tigran", "Tigran7", 2023),
new Person("Rebecca", "Rebecca-123", 2023),
new Person("Vidhi", "unknown", 2024)
];
function Classroom(teacher, students){
teacher.setRole("Teacher");
this.teacher = teacher;
this.classroom = [teacher];
this.students = students;
this.students.forEach(student => { student.setRole("Student"); this.classroom.push(student); });
this.json = [];
this.classroom.forEach(person => this.json.push(person.toJSON()));
}
compsci = new Classroom(teacher, students);
logItType(compsci.classroom);
logItType(compsci.classroom[0].name);
logItType(compsci.json[0]);
logItType(JSON.parse(compsci.json[0]));
// 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:black;" +
"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>" + "Period" + "</mark></th>";
body += "<th><mark>" + "Class Name" + "</mark></th>";
body += "<th><mark>" + "Room #" + "</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>";
// 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());