Console Log + String

  • console.log is used behind the scenes and is not visible from the browser.
console.log("Hello world!");
Hello world!

Console Log + String

  • console log also works without a string, and with a variable instead
var text = "Hello, World"; 
console.log(text);
Hello, World

Console.log + function

  • define function and call
function logIt(output) {
    console.log(output);
}
logIt(text);
Hello, World

Console.log + function (re use)

  • re use and call function logIt
console.log("Reuse of logIT")
logIt("Hey!");
logIt(1234)
Reuse of logIT
Hey!
1234

Dynamic language

  • js is a loosely types language which means you don't have to specific information type that will be stored into the variable
function logItType(output) {
    console.log(typeof output, ";", output);
}
console.log("Javascript is a loosely typed language: ")
logItType("hey") // String
logItType("1234") // Number
logItType([1,2,3,4]); // Object or Array
Javascript is a loosely typed language: 
string ; hey
string ; 1234
object ; [ 1, 2, 3, 4 ]

Build person function to save list

  • function Person allows to gain name, favorite color, and age data of the user
// define a function to hold data for a Person
function Person(name, favcolor, age) {
  this.name = name;
  this.favcolor = favcolor;
  this.age = age;
  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, favcolor: this.favcolor, age: this.age, role: this.role};
  const json = JSON.stringify(obj);
  return json;
}

// make a new Person and assign to variable teacher
var teacher = new Person("Mrs D", "neon yellow", 40);  // 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());
object ; Person { name: 'Mrs D', favcolor: 'neon yellow', age: 40, role: '' }
string ; {"name":"Mrs D","favcolor":"neon yellow","age":40,"role":""}
object ; Person {
  name: 'Mrs D',
  favcolor: 'neon yellow',
  age: 40,
  role: 'Teacher' }
string ; {"name":"Mrs D","favcolor":"neon yellow","age":40,"role":"Teacher"}

Build students and classroom array

  • var students is an array of all the students
  • forEach goes through the array and .push adds to it
// define a student Array of Person(s)
var students = [ 
    new Person("Tanisha", "purple", 15),
    new Person("Claire", "pink", 15),
    new Person("Amitha", "blue", 17),
    new Person("Grace", "sage", 17),
];

// 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
compsci = new Classroom(teacher, students);

// 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
object ; [ Person {
    name: 'Mrs D',
    favcolor: 'neon yellow',
    age: 40,
    role: 'Teacher' },
  Person { name: 'Tanisha', favcolor: 'purple', age: 15, role: 'Student' },
  Person { name: 'Claire', favcolor: 'pink', age: 15, role: 'Student' },
  Person { name: 'Amitha', favcolor: 'blue', age: 17, role: 'Student' },
  Person { name: 'Grace', favcolor: 'sage', age: 17, role: 'Student' } ]
string ; Mrs D
string ; {"name":"Mrs D","favcolor":"neon yellow","age":40,"role":"Teacher"}
object ; { name: 'Mrs D',
  favcolor: 'neon yellow',
  age: 40,
  role: 'Teacher' }

Table

  • var style is building the formatting of table
  • var body is creating the rows and headings. loop creates a new row for every person
// 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>" + "Favorite Color" + "</mark></th>";
    body += "<th><mark>" + "Age" + "</mark></th>";
    body += "<th><mark>" + "Role" + "</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.favcolor + "</td>";
      body += "<td>" + row.age + "</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());
</table></div> </div> </div> </div> </div> </div> </div>
NameFavorite ColorAgeRole
Mrs Dneon yellow40Teacher
Tanishapurple15Student
Clairepink15Student
Amithablue17Student
Gracesage17Student