// define a function to hold data for a Person
function Person(name, nationality, age) {
this.name = name;
this.nationality = nationality;
this.age = age;
this.role = "";
}
// define a setter for role in Person data
Person.prototype.setRole = function(role) {
this.role = role;
}
// defines the manager
var manager = new Person("Fortnite", "Fortnite", 70);
manager.setRole("Manager");
// JSON conversion from object to a string
Person.prototype.toJSON = function() {
const obj = {name: this.name, nationality: this.nationality, age: this.age, role: this.role};
const json = JSON.stringify(obj);
return json;
}
// player data
var players = [
new Person("Ty olsen", "Mexican", 16),
new Person("Khalid Farah", "Palestinian", 16),
new Person("Jeffery Fonseca", "African-American", 17),
new Person("Eli Gilmour", "Caucasian", 16),
new Person("Chris Albertson", "Syrian", 16),
new Person("Ryan McWeeny", "Caucasian", 16),
];
function Squad(Leader, players){
this.manager = Leader;
this.squad = [Leader];
this.players = players;
this.players.forEach(player => {player.setRole("Player"); this.squad.push(player); });
this.json = [];
this.squad.forEach(player => this.json.push(player.toJSON()));
}
var FUT = new Squad(manager, players);
Squad.prototype._toHtml = function() {
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>" + "Nationality" + "</mark></th>";
body += "<th><mark>" + "Age" + "</mark></th>";
body += "<th><mark>" + "Occupation" + "</mark></th>";
body += "</tr>";
// Data of Array, iterate through each row of lakers team
for (var row of FUT.squad) {
// tr for each row, a new line
body += "<tr>";
// td for each column of data
body += "<td>" + row.name + "</td>";
body += "<td>" + row.nationality + "</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(FUT._toHtml());