Rohan G JavaScript
Week 5 JS Data ig
var msg = "Hello, World!";
console.log(msg)
function logIt(output) {
console.log(output);
}
logIt(msg);
function logItType(output) {
console.log(typeof output, ";", output);
}
console.log("Looking at dynamic nature of types in JavaScript")
logItType("hello"); // String
logItType(2020); // Number
logItType([1, 2, 3]);
function Event(name, day, time) {
this.name = name;
this.day = day;
this.time = time;
this.role = "";
}
// define a setter for role in Event data
Event.prototype.setRole = function(role) {
this.role = role;
}
// define a JSON conversion "method" associated with Event
Event.prototype.toJSON = function() {
const obj = {name: this.name, day: this.day, time: this.time, role: this.role};
const json = JSON.stringify(obj); // json/string is useful when passing data on internet
return json;
}
// make a new Event and assign to variable geology
var geology = new Event("Dynamic Planet", "Friday", "5-6PM"); // object type is easy to work with in JavaScript
logItType(geology); // before role
logItType(geology.toJSON()); // ok to do this even though role is not yet defined
// output of Object and JSON/string associated with Teacher
geology.setRole("Teacher"); // set the role
logItType(geology);
logItType(geology.toJSON());
var events = [
new Event("Rocks and Minerals", "Tuesday", "5-6PM"),
new Event("Astronomy", "Tuesday", "6:30-8PM"),
new Event("Forestry", "Saturday", "10-11AM"),
new Event("Green Generation", "Sunday", "2-3PM"),
];
// define a classroom and build Classroom objects and json
function Classroom(geology, events){ // 1 geology, many student
// start Classroom with Teacher
geology.setRole("Best");
this.geology = geology;
this.classroom = [geology];
// add each Student to Classroom
this.events = events;
this.events.forEach(student => { student.setRole("mid"); this.classroom.push(student); });
// build json/string format of Classroom
this.json = [];
this.classroom.forEach(Event => this.json.push(Event.toJSON()));
}
// make a CompSci classroom from formerly defined geology and events
compsci = new Classroom(geology, events);
// 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>" + "Day" + "</mark></th>";
body += "<th><mark>" + "Time" + "</mark></th>";
body += "<th><mark>" + "Role" + "</mark></th>";
body += "</tr>";
// Data of Array, iterate through each row of compsci.classroom
for (var row in compsci.classroom) {
// tr for each row, a new line
body += "<tr>";
// td for each column of data
body += "<td>" + compsci.classroom[row].name + "</td>";
body += "<td>" + compsci.classroom[row].day + "</td>";
body += "<td>" + compsci.classroom[row].time + "</td>";
body += "<td>" + compsci.classroom[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());