-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
274 lines (247 loc) · 9.35 KB
/
app.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
const dinos = [
{
"species": "Triceratops",
"weight": 13000,
"height": 114,
"diet": "herbavor",
"where": "North America",
"when": "Late Cretaceous",
"fact": "First discovered in 1889 by Othniel Charles Marsh"
},
{
"species": "Tyrannosaurus Rex",
"weight": 11905,
"height": 144,
"diet": "carnivor",
"where": "North America",
"when": "Late Cretaceous",
"fact": "The largest known skull measures in at 5 feet long."
},
{
"species": "Anklyosaurus",
"weight": 10500,
"height": 55,
"diet": "herbavor",
"where": "North America",
"when": "Late Cretaceous",
"fact": "Anklyosaurus survived for approximately 135 million years."
},
{
"species": "Brachiosaurus",
"weight": 70000,
"height": "372",
"diet": "herbavor",
"where": "North America",
"when": "Late Jurasic",
"fact": "An asteroid was named 9954 Brachiosaurus in 1991."
},
{
"species": "Stegosaurus",
"weight": 11600,
"height": 79,
"diet": "herbavor",
"where": "North America, Europe, Asia",
"when": "Late Jurasic to Early Cretaceous",
"fact": "The Stegosaurus had between 17 and 22 seperate places and flat spines."
},
{
"species": "Elasmosaurus",
"weight": 16000,
"height": 59,
"diet": "carnivor",
"where": "North America",
"when": "Late Cretaceous",
"fact": "Elasmosaurus was a marine reptile first discovered in Kansas."
},
{
"species": "Pteranodon",
"weight": 44,
"height": 20,
"diet": "carnivor",
"where": "North America",
"when": "Late Cretaceous",
"fact": "Actually a flying reptile, the Pteranodon is not a dinosaur."
},
{
"species": "Pigeon",
"weight": 0.5,
"height": 9,
"diet": "herbavor",
"where": "World Wide",
"when": "Holocene",
"fact": "All birds are living dinosaurs."
}
]
// Create creature constructor - parent class
/**
* @description represents a creature base object
* @constructor
* @param {string} species - The species of the creature.
* @param {number} height - The height of the creature in inches.
* @param {number} weight - The weight of the creature in lbs.
* @param {string} diet - The diet of the creature.
*/
function Creature(species, height, weight, diet){
this.species = species;
this.height = Number.parseFloat(height);
this.weight = Number.parseFloat(weight);
this.diet = diet;
}
// Create Dino Constructor
/**
* @description represents a dinosaur object
* @constructor
* @param {object} dinoData - dino object with all data.
*/
function Dino(dinoData){
Creature.call(this, dinoData.species, dinoData.height, dinoData.weight, dinoData.diet);
this.where = dinoData.where;
this.when = dinoData.when;
this.fact = dinoData.fact;
};
// Create Dino Objects
/**
* @description An array which stores Dino Objects
*/
let dinosArray = [];
dinos.forEach(element => {
dinosArray.push(new Dino(element));
})
// Create Human Object
/**
* @description represents ad Human object
* @constructor
* @param {string} species
* @param {number} height
* @param {number} weight
* @param {string} diet
*/
function Human(species, height, weight, diet){
Creature.call(this, species, height, weight, diet);
}
// Use IIFE to get human data from form
const getHumanData = (function(){
const name = document.getElementById('name');
const feet = document.getElementById('feet');
const inches = document.getElementById('inches');
const weight = document.getElementById('weight');
const diet = document.getElementById('diet');
return function(){
const human = new Human(name.value, (Number.parseInt(feet.value)* 12)+ Number.parseInt(inches.value), weight.value, diet.value);
return human;
}
})();
// Create Dino Compare Method 1
// NOTE: Weight in JSON file is in lbs, height in inches.
Dino.prototype.compareDiet = function (humanDiet) {
return humanDiet.toLowerCase() == this.diet ? `${this.species} has the same diet as you.` : `
${this.species} is ${this.diet === "omnivor" ? "an" : 'a'} ${this.diet}.`
};
// Create Dino Compare Method 2
// NOTE: Weight in JSON file is in lbs, height in inches.
Dino.prototype.compareWeight = function (humanWeight) {
if (humanWeight === this.weight) {
return `${this.species} weight is equal to yours.`;
} else if(humanWeight < this.weight){
return `${this.species} weight is ${Math.floor(this.weight / humanWeight)} times bigger than yours.`;
} else if (humanWeight > this.weight) {
return `${this.species} weight is ${Math.floor(humanWeight / this.weight)} times less than yours.`;
}
};
// Create Dino Compare Method 3
// NOTE: Weight in JSON file is in lbs, height in inches.
Dino.prototype.compareHeight = function (humanHeight) {
if (humanHeight === this.height) {
return `${this.species} height is equal to yours.`;
} else if(humanHeight < this.height){
return `${this.species} height is ${Number(this.height / humanHeight).toFixed(2)} times bigger than yours.`;
} else if (humanHeight > this.height) {
return `${this.species} height is ${Number(humanHeight / this.height).toFixed(2)} times less than yours.`;
}
}
/**
* @description creates a single tile.
*
* @param {Object} creature An instance of a creature
*/
const createTile = (creature) => {
const {species, fact} = creature;
const isHuman = creature instanceof Human;
const card = document.createElement('div');
const h3 = document.createElement('h3');
const image = document.createElement('img');
const description = document.createElement('p');
/**
* @description returns creature name in lowercase.
* Handles human name change.
* @returns {string} name - creature name
*/
function imageName () {
const name = isHuman ? "human" : String(species).toLowerCase();
return name;
};
/**
* @description util function which return random fact
* @param {object} creature - a creature data
* @param {object} human - a human data
* @returns {string} fact - random dinos fact
*/
function generateFact (creature, human) {
// Generate random number from 1 - 6
const randomNumber = Math.floor(Math.random() * (6 - 1 + 1)) + 1;
if (creature.species == "Pigeon") {
return creature.fact
}
switch (randomNumber) {
case 1:
return creature.compareDiet(human.diet);
case 2:
return creature.compareWeight(human.weight);
case 3:
return creature.compareHeight(human.height);
case 4:
return creature.fact;
case 5:
return `${creature.species} lived in ${creature.when}`;
case 6:
return `${creature.species} lived in ${creature.where}`;
default:
return "default case";
}
}
image.src = `images/${imageName()}.png`;
h3.textContent = species;
description.textContent = generateFact(creature, getHumanData());
card.appendChild(h3);
card.appendChild(image);
// Generate description only for dino
!isHuman && card.appendChild(description);
card.classList.add('grid-item');
return card;
}
// Generate Tiles for each Dino in Array
/**
* @description Generate a tile grid
*/
const generateTiles = () => {
const grid = document.getElementById('grid');
const fragment = document.createDocumentFragment();
const dinosFirstPart = dinosArray.slice(0,4);
dinosFirstPart.push(getHumanData());
const dinosSecondPart = dinosArray.slice(4);
dinosArray = dinosFirstPart.concat(dinosSecondPart);
dinosArray.forEach(element => {
fragment.appendChild(createTile(element));
})
// Add tiles to DOM
grid.appendChild(fragment);
}
// On button click, prepare and display infographic
const button = document.getElementById("btn");
const prepareInfographic = () => {
const form = document.getElementById('dino-compare');
// Remove form from screen
form.style.display = 'none';
generateTiles();
}
button.addEventListener('click', prepareInfographic);