-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (53 loc) · 1.25 KB
/
index.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
59
60
61
62
const pets = [
{
name: 'kylo', species: 'dog', human: 'kari', mammal: true
},
{
name: 'gecky', species: 'lizard', human: 'dan', mammal: false
},
{
name: 'hedwig', species: 'owl', human: 'harry', mammal: false
},
{
name: 'crookshanks', species: 'cat', human: 'hermione', mammal: true
},
{
name: 'scabbers', species: 'rat', human: 'ron', mammal: true
},
];
console.log("Running our javascript file");
const buildHeaders = () => {
let row = '<tr>';
Object.keys(pets[0]).forEach((key) => {
row += '<th>' + key + '</th>';
});
row += '</tr>';
$('#pet-table thead').html(row);
};
const buildBody = () => {
console.log('About to build table body');
pets.forEach((pet) => {
// Start with an emtpy string
let row = '';
// Add in the starting <tr> tag, possibly
// with a class
if (pet['mammal']) {
row += '<tr class="mammal">';
} else {
row += '<tr>';
}
// Add in each of the table cells for this pet
for (const key in pet) {
row += '<td>' + pet[key] + '</td>';
}
// Closing </tr> tag
row += '</tr>';
console.log(row);
// Add it to the DOM
$('#pet-table tbody').append(row);
});
};
$(document).ready(() => {
buildHeaders();
buildBody();
});