# JAVASCRIPT

- toc: true
- badges: true
- comments: true
- categories: [Java, markdown]
console.log("Hello World my name is Ahad Biabani");
Hello World my name is Ahad Biabani

Using Variable in console.log

var ahad = "Hello fortnite hi hi hi";
console.log(ahad);
Hello fortnite hi hi hi

Output showing use of function

function logIt(output) {
    console.log(output);
}
logIt(ahad);
Hello fortnite hi hi hi

Showing reuse of fucntion

console.log("Reuse of logIT")
logIt("I like to play Ice Hockey");
logIt(2005)
Reuse of logIT
I like to play Ice Hockey
2005

Dynamic or Loosely typed language (string, number)

function logItType(output) {
    console.log(typeof output, ";", output);
}
console.log("Ahad is the best APCSP student")
logItType("hello"); 
logItType(2005);    
logItType([1, 2, 3, 4, 5]);
Ahad is the best APCSP student
string ; hello
number ; 2005
object ; [ 1, 2, 3, 4, 5 ]

Build a Person Function/Class object and JSON

function Person(name, ghID, classOf) {
    this.name = name;
    this.ghID = ghID;
    this.classOf = classOf;
    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, classOf: this.classOf, role: this.role};
    const json = JSON.stringify(obj);
    return json;
}

// make a new Person and assign to variable teacher
var student = new Person("Ahad Biabani", "Ahadb63", 2024);  // object type is easy to work with in JavaScript
logItType(student);  // before role
logItType(student.toJSON());  // ok to do this even though role is not yet defined

// output of Object and JSON/string associated with Teacher
student.setRole("Teacher");   // set the role
logItType(student); 
logItType(student.toJSON());
object ; Person { name: 'Ahad Biabani', ghID: 'Ahadb63', classOf: 2024, role: '' }
string ; {"name":"Ahad Biabani","ghID":"Ahadb63","classOf":2024,"role":""}
object ; Person {
  name: 'Ahad Biabani',
  ghID: 'Ahadb63',
  classOf: 2024,
  role: 'Teacher' }
string ; {"name":"Ahad Biabani","ghID":"Ahadb63","classOf":2024,"role":"Teacher"}

Build a Classroom Array/List of Persons and JSON

var students = [ 
    new Person("Kian", "Pasokhi", 2024),
    new Person("Khalid", "Farah", 2024),
    new Person("Neel", "Agnihotri", 2024),
];

// 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]));
evalmachine.<anonymous>:22
compsci = new Classroom(teacher, students);
                        ^

ReferenceError: teacher is not defined
    at evalmachine.<anonymous>:22:25
    at ContextifyScript.Script.runInThisContext (vm.js:25:33)
    at Object.runInThisContext (vm.js:97:38)
    at run ([eval]:1020:15)
    at onRunRequest ([eval]:864:18)
    at onMessage ([eval]:828:13)
    at emitTwo (events.js:106:13)
    at process.emit (events.js:191:7)
    at process.nextTick (internal/child_process.js:758:12)
    at _combinedTickCallback (internal/process/next_tick.js:73:7)

IJavaScript and Table formatting using toHTML method

Classroom.prototype._toHtml = function() {
    // HTML Style is build using inline structure
    var style = (
      "display:inline-block;" +
      "background:black;" +
      "border: 2px solid white;" +
      "box-shadow: 0.8em 0.4em 0.4em blue;"
    );
  
    // 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>" + "GitHub ID" + "</mark></th>";
    body += "<th><mark>" + "Class Of" + "</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.ghID + "</td>";
      body += "<td>" + row.classOf + "</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>
NameGitHub IDClass OfRole
Ahad BiabaniAhadb632024Teacher
KianPasokhi2024Student
KhalidFarah2024Student
NeelAgnihotri2024Student