JavaScript Notebook
console.log("Yo");
var x = "Yo";
console.log(x);
for(i = 0; i <= 10; i++){
console.log(i)
}
var age = "16";
console.log(age);
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]);
function Person(name, ghID, classOf) {
this.name = name;
this.ghID = ghID;
this.classOf = classOf;
this.role = "";
}
Person.prototype.setRole = function(role) {
this.role = role;
}
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;
}
var teacher = new Person("Mr M", "jm1021", 1977);
logItType(teacher);
logItType(teacher.toJSON());
teacher.setRole("Teacher");
logItType(teacher);
logItType(teacher.toJSON());
var students = [
new Person("Dillon", "dillonlee06", 2024),
new Person("Rohan", "RohanAgr", 2023),
new Person("Derek", "DerekSol", 2023),
new Person("Saavan", "SaavanGade", 2023),
];
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]));
Classroom.prototype._toHtml = function() {
var style = (
"display:inline-block;" +
"border: 2px solid grey;" +
"box-shadow: 0.8em 0.4em 0.4em grey;"
);
var body = "";
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 += "</tr>";
for (var row of compsci.classroom) {
body += "<tr>";
body += "<td>" + row.name + "</td>";
body += "<td>" + row.ghID + "</td>";
body += "<td>" + row.classOf + "</td>";
body += "<td>" + row.role + "</td>";
body += "<tr>";
}
return (
"<div style='" + style + "'>" +
"<table>" +
body +
"</table>" +
"</div>"
);
};
$$.html(compsci._toHtml());