console.log output

console.log command outputs the parameter to the console, so that you can see it in the Jupyter document.

console.log("Hello, World!");
Hello, World!

console.log output showing use of variable

This second example is a sequence of code, two or more lines forms a sequence. This example defines a variable, then outputs the msg to terminal.

var msg = "Hello, Kalani!";
console.log(msg);
Hello, Kalani!

console.log output showing use of a function

This example passes the previously defined variable "msg" to the newly defined "function logIt(output)".

function logIt(output) {
    console.log(output);
}
logIt("yo");
yo

Showing reuse of a function

Now that a function is defined, it can be called from any of the subsequent cell in the Jupyter notebook. A function/method, is a process of creating a procedural abstraction. This a programming practice to promote reuse versus coding the same thing over and over.

  • First call sends a different string message
  • Second call sends a number
console.log("Reuse of logIT")
logIt("Hello, Students!");
logIt(2022)
Reuse of logIT
Hello, Students!
2022

Dynamic or Loosely typed language (string, number)

JavaScript is a loosely typed language, don't have to specify what type of information will be stored in a variable in advance. This is similar to Python and most interpretive languages. Java which is a compiled language is strongly typed, thus you will see string, integer, double, and object in the source code. In JavaScript, the "typeof" keyword returns the type.

function logItType(output) {
    console.log(typeof output, ";", output);
}
console.log("dynamic nature of types in JavaScript")
logItType("hello bro"); // String
logItType(2020);    // Number
logItType([1, 2, 3]);  // Object is generic for this Array, which similar to Python List
dynamic nature of types in JavaScript
string ; hello bro
number ; 2020
object ; [ 1, 2, 3 ]

Build a Person Function/Group object and JSON

JavaScript functions have special properties and syntax is shown in many ways. In fact, a Class in JavaScript is a special function. Jupyter Notebooks seems to be more friendly to "function" definitions versus "Class", thus this lesson uses "function" and "prototype" versus "Class".

  • Definition of function allows for a collection of data
  • Definition of a prototype allow for the definition of a method associated with the function
  • Instance of a function
// define a function to hold data for a Person
function Person(name, sport, age) {
    this.name = name;
    this.sport = sport;
    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, sport: this.sport, age: this.age, role: this.role};
    const json = JSON.stringify(obj);
    return json;
}

// make a new Person and assign to variable teacher
var thegoat = new Person("Kalani", "soccer", 16);
thegoat.setRole("GOAT");

// output of Object and JSON/string associated with Teacher
logItType(thegoat);  // object type is easy to work with in JavaScript
logItType(thegoat.toJSON());  // json/string is useful when passing data on internet
object ; Person { name: 'Kalani', sport: 'soccer', age: 16, role: 'GOAT' }
string ; {"name":"Kalani","sport":"soccer","age":16,"role":"GOAT"}

Build a Classroom Array/List of Persons and JSON

Many key elements are shown again. New elements include...

  • Building an Array, "var students" is an array of many persons
  • Building a Classroom, this show forEach iteration through an array and .push adding to an array. These are key concepts in all programming languages.
// define a student Array of Person(s)
var students = [ 
    new Person("Navan", "football", 15),
    new Person("Alex", "basketball", 16),
    new Person("Safin", "tennis", 16),
];

// define a classroom and build Classroom objects and json
function Classroom(thegoat, students){ // 1 teacher, many student
    // start Classroom with Teacher
    thegoat.setRole("GOAT");
    this.thegoat = thegoat;
    this.classroom = [thegoat];
    // 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(thegoat, 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: 'Kalani', sport: 'soccer', age: 16, role: 'GOAT' },
  Person { name: 'Navan', sport: 'football', age: 15, role: 'Student' },
  Person { name: 'Alex', sport: 'basketball', age: 16, role: 'Student' },
  Person { name: 'Safin', sport: 'tennis', age: 16, role: 'Student' } ]
string ; Kalani
string ; {"name":"Kalani","sport":"soccer","age":16,"role":"GOAT"}
object ; { name: 'Kalani', sport: 'soccer', age: 16, role: 'GOAT' }

IJavaScript and Table formatting using toHTML method

This example builds a Classroom method _toHTML which is passed to the IJavaScript interpreter $$.html which renders output similarly to a real website.

  • JavaScript in the _toHTML method is broken into three parts...
    • Style part is building CSS inline formatting
    • Body part is constructing the Table Rows (tr), Table Headings (th), and Table Data (td). The table data is obtained from a Classroom object. The JavaScript for loop allows the construction of a new row of data for each person object in the Array.
    • Return part creates the HTML fragment for rendering
  • The last line in the example $$.html is IJavaScript HTML interpreter and by passing the parameter of the _toHTML method it obtains HTML to render.
// define an HTML conversion "method" associated with Classroom
Classroom.prototype._toHtml = function() {
  // HTML Style is build using inline structure
  var style = (
    "display:inline-block;" +
    "background:black;" +
    "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>" + "Sport" + "</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.sport + "</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>
Name Sport age Role
Kalani soccer 16 GOAT
Navan football 15 Student
Alex basketball 16 Student
Safin tennis 16 Student