JavaScript
This notebook demonstrates my understanding of basic JavaScript syntax
var text = "Hello World";
console.log(text);
function Print(Text) {
console.log(Text);
}
Print(text);
function PrintType(output) {
console.log(typeof output, ", ", output);
}
PrintType("string");
PrintType(1);
PrintType(true);
PrintType(["A","B","C"]);
var addition = 1+1;
console.log(addition);
var subtraction = 1-1;
console.log(subtraction);
var multiplication = 1*2;
console.log(multiplication);
var division = 1/2;
console.log(division);
var modulus = 3%2; // division remainder
console.log(modulus);
// define a function to hold data for a Person
function Person(name, age, residence) {
this.name = name;
this.age = age;
this.residence = residence;
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, age: this.age, residence: this.residence, role: this.role};
const json = JSON.stringify(obj);
return json;
}
// make a new Person and assign to variable student
var student = new Person("Ryan", 16, "SanDiego"); // object type is easy to work with in JavaScript
PrintType(student); // before role
PrintType(student.toJSON());
// output of Object and JSON/string associated with student
student.setRole("Student"); // set the role
PrintType(student);
PrintType(student.toJSON());