{'cells': [{'cell_type': 'markdown',
'metadata': {},
'source': ['# JavaScript\n',
'> This notebook presents my understanding of JavaScript \n',
'\n',
'- toc: true\n',
'- branch: master\n',
'- badges: true\n',
'- comments: true\n',
'- author: Chris Albertson\n',
'- categories: [week5]']},
{'cell_type': 'markdown',
'metadata': {},
'source': ['{% include frontend.html %}']},
{'cell_type': 'code',
'execution_count': 13,
'metadata': {},
'outputs': [{'name': 'stdout',
'output_type': 'stream',
'text': ["object , Person { name: 'Chris', age: 16, residence: 'SanDiego', role: '' }\n",
'string , {"name":"Chris","age":16,"residence":"SanDiego","role":""}\n',
"object , Person { name: 'Chris', age: 16, residence: 'SanDiego', role: 'Student' }\n",
'string , {"name":"Chris","age":16,"residence":"SanDiego","role":"Student"}\n']}],
'source': ['function PrintType(output) { // function to output data type and data into terminal\n',
' console.log(typeof output, ", ", output);\n',
'}\n',
'// define a function to hold data for a Person\n',
'function Person(name, age, residence) {\n',
' this.name = name;\n',
' this.age = age;\n',
' this.residence = residence;\n',
' this.role = "";\n',
'}\n',
'\n',
'// define a setter for role in Person data\n',
'Person.prototype.setRole = function(role) {\n',
' this.role = role;\n',
'}\n',
'\n',
'// define a JSON conversion "method" associated with Person\n',
'Person.prototype.toJSON = function() {\n',
' const obj = {name: this.name, age: this.age, residence: this.residence, role: this.role};\n',
' const json = JSON.stringify(obj);\n',
' return json;\n',
'}\n',
'\n',
'// make a new Person and assign to variable student\n',
'var student = new Person("Ryan", 16, "SanDiego"); // object type is easy to work with in JavaScript\n',
'PrintType(student); // before role\n',
'PrintType(student.toJSON());\n',
'\n',
'// output of Object and JSON/string associated with student\n',
'student.setRole("Student"); // set the role\n',
'PrintType(student); \n',
'PrintType(student.toJSON()); ']},
{'cell_type': 'markdown', 'metadata': {}, 'source': []},
{'cell_type': 'code',
'execution_count': 15,
'metadata': {},
'outputs': [{'name': 'stdout',
'output_type': 'stream',
'text': ['object , [ Person {\n',
" name: 'Mr. Mort',\n",
" age: '52',\n",
" residence: 'San Diego',\n",
" role: 'Teacher' },\n",
" Person { name: 'Khalid', age: 16, residence: 'San Diego', role: 'Student' },\n",
" Person { name: 'Ryan', age: 16, residence: 'San Diego', role: 'Student' },\n",
" Person { name: 'Josh', age: 16, residence: 'San Diego', role: 'Student' },\n",
' Person {\n',
" name: 'Jeffery',\n",
' age: 17,\n',
" residence: 'San Diego',\n",
" role: 'Student' } ]\n",
'string , Mr. Mort\n',
'string , {"name":"Mr. Mort","age":"52","residence":"San Diego","role":"Teacher"}\n',
"object , { name: 'Mr. Mort',\n",
" age: '52',\n",
" residence: 'San Diego',\n",
" role: 'Teacher' }\n"]}],
'source': ['// define a student Array of Person(s)\n',
'var students = [ \n',
' new Person("Khalid", 16, "San Diego"),\n',
' new Person("Ryan", 16, "San Diego"),\n',
' new Person("Josh", 16, "San Diego"),\n',
' new Person("Jeffery", 17, "San Diego")\n',
'];\n',
'var teacher = new Person("Mr. Mort", "52", "San Diego")\n',
'\n',
'// define a classroom and build Classroom objects and json\n',
'function Classroom(teacher, students){ // 1 teacher, many student\n',
' // start Classroom with Teacher\n',
' teacher.setRole("Teacher");\n',
' this.teacher = teacher;\n',
' this.classroom = [teacher];\n',
' // add each Student to Classroom\n',
' this.students = students;\n',
' this.students.forEach(student => { student.setRole("Student"); this.classroom.push(student); });\n',
' // build json/string format of Classroom\n',
' this.json = [];\n',
' this.classroom.forEach(person => this.json.push(person.toJSON()));\n',
'}\n',
'\n',
'// make a CompSci classroom from formerly defined teacher and students\n',
'compsci = new Classroom(teacher, students);\n',
'\n',
'// output of Objects and JSON in CompSci classroom\n',
'PrintType(compsci.classroom); // constructed classroom object\n',
'PrintType(compsci.classroom[0].name); // abstract 1st objects name\n',
'PrintType(compsci.json[0]); // show json conversion of 1st object to string\n',
'PrintType(JSON.parse(compsci.json[0])); // show JSON.parse inverse of JSON.stringify']},
{'cell_type': 'code',
'execution_count': 16,
'metadata': {},
'outputs': [{'data': {'text/html': ["<div style='display:inline-block;border: 2px solid grey;box-shadow: 0.8em 0.4em 0.4em grey;'><table><tr><th><mark>Name</mark></th><th><mark>Age</mark></th><th><mark>Residence</mark></th><th><mark>Role</mark></th></tr><tr><td>Mr. M</td><td>?</td><td>San Diego</td><td>Teacher</td><tr><tr><td>Ryan</td><td>16</td><td>San Diego</td><td>Student</td><tr><tr><td>Josh</td><td>16</td><td>San Diego</td><td>Student</td><tr><tr><td>Eli</td><td>16</td><td>San Diego</td><td>Student</td><tr><tr><td>Jeffery</td><td>17</td><td>San Diego</td><td>Student</td><tr></table></div>"]},
'execution_count': 16,
'metadata': {},
'output_type': 'execute_result'}],
'source': ['// define an HTML conversion "method" associated with Classroom\n',
'Classroom.prototype._toHtml = function() {\n',
' // HTML Style is build using inline structure\n',
' var style = (\n',
' "display:inline-block;" +\n',
' "border: 2px solid grey;" +\n',
' "box-shadow: 0.8em 0.4em 0.4em grey;"\n',
' );\n',
' \n',
' // HTML Body of Table is build as a series of concatenations (+=)\n',
' var body = "";\n',
' // Heading for Array Columns\n',
' body += "<tr>";\n',
' body += "<th><mark>" + "Name" + "</mark></th>";\n',
' body += "<th><mark>" + "Age" + "</mark></th>";\n',
' body += "<th><mark>" + "Residence" + "</mark></th>";\n',
' body += "<th><mark>" + "Role" + "</mark></th>";\n',
' body += "</tr>";\n',
' // Data of Array, iterate through each row of compsci.classroom \n',
' for (var row of compsci.classroom) {\n',
' // tr for each row, a new line\n',
' body += "<tr>";\n',
' // td for each column of data\n',
' body += "<td>" + row.name + "</td>";\n',
' body += "<td>" + row.age + "</td>";\n',
' body += "<td>" + row.residence + "</td>";\n',
' body += "<td>" + row.role + "</td>";\n',
' // tr to end line\n',
' body += "<tr>";\n',
' }\n',
' \n',
' // Build and HTML fragment of div, table, table body\n',
' return (\n',
' "<div style=\'" + style + "\'>" +\n',
' "<table>" +\n',
' body +\n',
' "</table>" +\n',
' "</div>"\n',
' );\n',
' \n',
' };\n',
' \n',
' // IJavaScript HTML processor receive parameter of defined HTML fragment\n',
' $$.html(compsci._toHtml());']}],
'metadata': {'kernelspec': {'display_name': 'JavaScript (Node.js)',
'language': 'javascript',
'name': 'javascript'},
'language_info': {'file_extension': '.js',
'mimetype': 'application/javascript',
'name': 'javascript',
'version': '6.11.2'},
'orig_nbformat': 4,
'vscode': {'interpreter': {'hash': '916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1'}}},
'nbformat': 4,
'nbformat_minor': 2}