JavaScript Tutorial
JavaScript Tutorial
// Print Hello World
console.log("Hello World!");
// Print Using var assignment
var text = "AP CSA IS GREAT!!";
console.log(text);
// Print Using Functions
var message = "Class of 2022"
function print(output) {
console.log(output);
}
print(message);
// Print Reusing Functions
print("RAYJ is the best team!");
function printType(output) {
console.log(typeof output + ": " + output);
}
printType("AP CSA");
printType(2023);
printType([1, 2, 3]);
// define a function to hold data for a Person
function Person(name, song, artist, releaseYear) {
this.name = name;
this.song = song;
this.artist = artist;
this.releaseYear = releaseYear;
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, song: this.song, artist: this.artist, releaseYear: this.releaseYear, role: this.role};
const json = JSON.stringify(obj); // json/string is useful when passing data on internet
return json;
}
// make a new Person and assign to variable sponsor
var sponsor = new Person("Mr. M", "Stayin' Alive", "Bee Gees", 1977); // object type is easy to work with in JavaScript
printType(sponsor); // before role
printType(sponsor.toJSON()); // ok to do this even though role is not yet defined
// output of Object and JSON/string associated with sponsor
sponsor.setRole("Sponsor"); // set the role
printType(sponsor);
printType(sponsor.toJSON());
// 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, song: this.song, artist: this.artist, releaseYear: this.releaseYear, role: this.role};
const json = JSON.stringify(obj); // json/string is useful when passing data on internet
return json;
}
// make a new Person and assign to variable sponsor
var sponsor = new Person("Mr. M", "Stayin' Alive", "Bee Gees", 1977); // object type is easy to work with in JavaScript
printType(sponsor.toJSON()); // ok to do this even though role is not yet defined
// output of Object and JSON/string associated with sponsor
sponsor.setRole("Sponsor"); // set the role
printType(sponsor.toJSON());
// define a student Array of Person(s)
var teammate = [
new Person("Rithwikh", "Drop It Like It's Hot", "Snoop Dogg", 2004),
new Person("Aidan", "Ms. Jackson", "OutKast", 2000),
new Person("Jun", "Ignition", "R. Kelly", 2002),
new Person("Yash", "In Da Club", "50 Cent", 2003)
];
// define a Team and build Team objects and json
function Team(sponsor, teammate){ // 1 sponsor, many student
// start Team with sponsor
sponsor.setRole("Sponsor");
this.sponsor = sponsor;
this.Team = [sponsor];
// add each Student to Team
this.teammate = teammate;
this.teammate.forEach(teammate => { teammate.setRole("Teammate"); this.Team.push(teammate); });
// build json/string format of Team
this.json = [];
this.Team.forEach(person => this.json.push(person.toJSON()));
}
// make a CompSci Team from formerly defined sponsor and students
compsci = new Team(sponsor, teammate);
// define an HTML conversion "method" associated with Team
Team.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><strong>" + "Name" + "</strong></th>";
body += "<th><strong>" + "Age" + "</strong></th>";
body += "<th><strong>" + "Artist" + "</strong></th>";
body += "<th><strong>" + "Release Year" + "</strong></th>";
body += "<th><strong>" + "Role" + "</strong></th>";
body += "</tr>";
// Data of Array, iterate through each row of compsci.Team
for (var row in compsci.Team) {
// tr for each row, a new line
body += "<tr>";
// td for each column of data
body += "<td>" + compsci.Team[row].name + "</td>";
body += "<td>" + compsci.Team[row].song + "</td>";
body += "<td>" + compsci.Team[row].artist + "</td>";
body += "<td>" + compsci.Team[row].releaseYear + "</td>";
body += "<td>" + compsci.Team[row].role + "</td>";
// tr to end line
body += "<tr>";
}
// Build and HTML fragment of div, table, table body
return ("<div style='" + "'>" + "<table>" + body + "</table>" + "</div>");
};
// IJavaScript HTML processor receive parameter of defined HTML fragment
$$.html(compsci._toHtml());