-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
195 lines (180 loc) · 5.83 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
const dinosData = [
{
"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 Dino Constructor
function Dino(species, weight, diet, fact) {
this.species = species;
this.weight = weight;
this.diet = diet;
this.facts = [fact];
this.image = "images/" + species.toLowerCase() + ".png";
}
Dino.prototype.addFact = function (fact) {
this.facts.push(fact);
};
// compare method 1
Dino.prototype.compareNameLength = function (name) {
let fact = "The length of my species name is same as your name.";
if (this.species.length > name.length) {
fact = "The length of my species name is longer than your name.";
} else if (this.species.length < name.length) {
fact = "The length of my species name is shorter than your name.";
}
this.addFact(fact);
};
// compare method 2
Dino.prototype.compareWeight = function (weight) {
let fact = "Our weight is exactly same!";
if (this.weight > weight) {
fact = "I'm heavier than you!";
} else if (this.weight < weight) {
fact = "I'm lighter than you...";
}
this.addFact(fact);
};
// compare method 3
Dino.prototype.compareDiet = function (diet) {
let fact = "We don't eat same type of food...";
if (this.diet === diet) {
fact = "We eat same type of food!";
}
this.addFact(fact);
};
// Create Dino Objects
let dinos = dinosData.map(function (value) {
const species = value.species;
const weight = value.weight;
const diet = value.diet;
const fact = value.fact;
return new Dino(species, weight, diet, fact);
});
// procedure aftrer clincking button
document.getElementById("btn").addEventListener("click", function(){
// Create Human Object
const human = (function(){
let name = document.getElementById("name");
let feet = document.getElementById("feet");
let height = document.getElementById("height");
let weight = document.getElementById("weight");
let diet = document.getElementById("diet");
return {
"species" : "human",
"weight" : weight.value,
"diet" : diet.value,
"fact" : name.value,
"image" : "images/human.png"
}
})();
// add new fact to every dinos and create tiles on DOM
for (let i = 0; i < 8; i++){
dinos[i].compareNameLength(human.fact);
dinos[i].compareWeight(human.weight);
dinos[i].compareDiet(human.diet);
let fact =
dinos[i].facts[Math.floor(Math.random()*dinos[i].facts.length)];
// in case of birds
if (dinos[i].weight < 1) {
fact = "All birds are Dinosaurs.";
}
const imgUrl =
"images/" + dinosData[i]["species"].toLowerCase() + ".png"
gridItemDiv = getGridItem(dinosData[i].species, imgUrl, fact)
document.getElementById("grid").appendChild(gridItemDiv);
// add human tile
if (i===3){
gridItemDiv = getGridItem(human.species, human.image, human.fact)
document.getElementById("grid").appendChild(gridItemDiv);
};
};
// Remove form from screen
document.getElementById("dino-compare").style.display = "none";
})
// define to create grid tiles as generic code
function getGridItem(species, imgUrl, fact){
let gridItemDiv = document.createElement("div");
gridItemDiv.className = "grid-item";
//add species
let speciesDiv = document.createElement("h3");
speciesDiv.innerText = species;
gridItemDiv.appendChild(speciesDiv);
//add image
let imageDiv = document.createElement("img");
imageDiv.src = imgUrl
gridItemDiv.appendChild(imageDiv);
let factDiv = document.createElement("p");
factDiv.innerText = fact;
gridItemDiv.appendChild(factDiv);
return gridItemDiv;
}