-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_3.js
58 lines (49 loc) · 1.41 KB
/
Problem_3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
|--------------------------------------------------
| 3. Create a class called TableGenerator, which contains data and title properties.
Develop render method which generates html markup as table for given input data.
Input:
let data = [
{title: 'apple', price: 2, qty: 30},
{title: 'banana', price: 1, qty: 30},
{title: 'chikoo', price: 1, qty: 30}
];
title = “fruits”
|--------------------------------------------------
*/
let data = [
{ title: "apple", price: 2, qty: 30 },
{ title: "banana", price: 1, qty: 30 },
{ title: "chikoo", price: 1, qty: 30 }
],
title = "fruits";
class TableGenerator {
constructor(data, title) {
this._data = data;
this._title = title;
}
generateTableHeader() {
return `<thead><tr><th>Title</th><th>Price</th><th>Qty</th></tr></thead>`;
}
generateTableBody() {
let body = "";
this._data.map(
x =>
(body += `<tr><td>${x.title}</td><td>${x.price}</td><td>${
x.qty
}</td></tr>`)
);
return body;
}
render() {
console.log("====================================");
console.log(this._title);
console.log("====================================");
console.log();
let table = `<table>${this.generateTableHeader()}${this.generateTableBody()}</table>`;
console.log(table);
document.body.innerHTML = table;
}
}
var table = new TableGenerator(data, title);
table.render();