Javascript Notebook
Experimenting with Javascript
function sum(a, b) {
return a + b;
}
console.log(sum(2, 11))
var x;
x = 5;
function multiply(x, b) {
return x * b;
}
console.log(multiply(x, 10));
function logItType(output) {
console.log(typeof output, ";", output);
}
console.log("")
logItType("hello");
logItType(2022);
logItType([1, 2, 3]);
function Person(name, ghID, teamrole) {
this.name = name;
this.ghID = ghID;
this.teamrole = teamrole;
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, teamrole: this.teamrole, 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", "Grader"); // object type is easy to work with in JavaScript
logItType(teacher); // before role
logItType(teacher.toJSON()); // ok to do this even though role is not yet defined
// output of Object and JSON/string associated with Teacher
teacher.setRole("Teacher"); // set the role
logItType(teacher);
logItType(teacher.toJSON());
var students = [
new Person("Luka", "LukaVDB", "DevOps"),
new Person("Jishnu", "JishnuS420", "Backend Developer"),
new Person("Edwin", "EdwinKuttappi", "Frontend Developer"),
new Person("Emaad", "Emaad-Mir", "Scrum Master"),
];
// 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
csp = new Classroom(teacher, students);
// output of Objects and JSON in CompSci classroom
logItType(csp.classroom); // constructed classroom object
logItType(csp.classroom[0].name); // abstract 1st objects name
logItType(csp.json[0]); // show json conversion of 1st object to string
logItType(JSON.parse(csp.json[0]));
Classroom.prototype._toHtml = function() {
// HTML Style is build using inline structure
var style = (
"display:inline-block;" +
"border: 2px solid blue;"
);
// HTML Body of Table is build as a series of concatenations (+=)
var body = "";
// Heading for Array Columns
body += "<tr>";
body += "<th>" + "Name" + "</th>";
body += "<th>" + "GitHub ID" + "</th>";
body += "<th>" + "Team Role" + "</th>";
body += "<th>" + "Class Role" + "</th>";
body += "</tr>";
// Data of Array, iterate through each row of compsci.classroom
for (var row of csp.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.teamrole + "</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(csp._toHtml());