-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·214 lines (191 loc) · 7.01 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
/* eslint-env browser */
document.addEventListener("DOMContentLoaded", () => {
"use strict";
(() => {
const xmlhttp = new XMLHttpRequest();
const dinos = [];
const formSubmit = document.getElementById("btn");
/**
* @description Represents an animal. This conscturctor is used for dinos and humans since both are animals
* @constructor
* @param {string} species - The species of the animal
* @param {number} weight - The weight of the animal
* @param {number} height - The height of the animal
* @param {string} diet - The diet of the animal
* @param {string} where - The geographical area where the animal lived
* @param {string} when - The ecological period when the animal lived
* @param {string} fact - An interesting fact about the animal
*/
function Animal(species, weight, height, diet, where, when, fact) {
this.species = species;
this.weight = weight;
this.height = height;
this.diet = diet;
this.where = where ? where.toLowerCase() : "";
this.when = when ? when.toLowerCase() : "";
this.fact = fact;
this.facts = [this.fact];
}
/**
* @description Compares the animal to a human
* @returns {array} Three facts resulting from the comparison
*/
Animal.prototype.compareToHuman = function () {
return [this.compareHeight(), this.compareWeight(), this.compareDiet()];
};
/**
* @description Compares the animal's height to a human's height
* @returns {string} A fact resulting from the height comparison
*/
Animal.prototype.compareHeight = function () {
const heightDiff = this.height - human.height;
const heightComp = heightDiff >= 0 ? "taller" : "shorter";
return `A ${this.species} is ${Math.abs(
heightDiff
)} inches ${heightComp} than ${human.name}`;
};
/**
* @description Compares the animal's weight to a human's weight
* @returns {string} A fact resulting from the weight comparison
*/
Animal.prototype.compareWeight = function () {
const weightDiff = this.weight - human.weight;
const weightComp = weightDiff >= 0 ? "heavier" : "lighter";
return `A ${this.species} is ${Math.abs(
weightDiff
)} pounds ${weightComp} than ${human.name}`;
};
/**
* @description Compares the animal's diet to a human's diet
* @returns {string} A fact resulting from the diet comparison
*/
Animal.prototype.compareDiet = function () {
const dietComp =
this.diet === human.diet
? `just like ${human.name}`
: `while ${human.name} is a ${human.diet}`;
return `A ${this.species} is a ${this.diet} ${dietComp}`;
};
/**
* @description Selects a random fact from an array of facts
* @returns {string} A fact from the facts array
*/
Animal.prototype.getRandomFact = function () {
return this.facts[Math.floor(Math.random() * this.facts.length)];
};
/**
* @description Adds five facts to the facts array
*/
Animal.prototype.setFacts = function () {
if (this.species !== "Pigeon") {
this.facts.push(`The ${this.species} lived in ${this.where}`);
this.facts.push(`The ${this.species} lived in ${this.when} period`);
this.facts.push(...this.compareToHuman());
}
};
/**
* @description Gets the url for the animal's image
* @returns {string} The url for the animal's image
*/
Animal.prototype.getImageUrl = function () {
return (
"./images/" + this.species.toLowerCase().split(" ").join("_") + ".png"
);
};
/**
* @description Gets the HTML for the animal's tile
* @returns {string} The HTML for the animal's tile
*/
Animal.prototype.getTileHTML = function () {
const tileTitle =
this.species === "human"
? `<h3>${this.name}</h3>`
: `<h3>${this.species}</h3>`;
const tileImg = `<img src="${this.getImageUrl()}" alt="Image rendering of a ${
this.species
}">`;
const tileFact =
this.species === "human" ? `` : `<p>${this.getRandomFact()}</p>`;
return `<div class="grid-item">${tileTitle}${tileImg}${tileFact}</div>`;
};
/**
* @description Parses the JSON file and creates dinosaur objects
*/
xmlhttp.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
const dinoData = JSON.parse(this.responseText).Dinos;
dinoData.forEach((dino) => {
dinos.push(
new Animal(
dino.species,
dino.weight,
dino.height,
dino.diet,
dino.where,
dino.when,
dino.fact
)
);
});
}
};
xmlhttp.open("GET", "./dino.json", true);
xmlhttp.send();
/**
* @description Randomly orders the dinos array, but makes sure that the pigon object
* remains last after the reordering and adds the human object to the
* middle position
*/
const randomlyOrderDinos = () => {
const pigeon = dinos.pop();
for (let i = dinos.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[dinos[i], dinos[j]] = [dinos[j], dinos[i]];
}
dinos.push(pigeon);
dinos.splice(4, 0, human);
};
/**
* @description Adds the property values from the form to the human object
* @param {string} name - The human's name
* @param {number} feet - The feet part for the human height
* @param {number} inches - The inches part for the human height
* @param {number} weight - The human's weight
* @param {string} diet - The human's diet
*/
const addPropsToHuman = (name, feet, inches, weight, diet) => {
human.name = name.charAt(0).toUpperCase() + name.slice(1);
human.height = feet * 12 + inches;
human.weight = weight;
human.diet = diet;
};
/**
* @description Returns the grid's HTML
* @return {string} html - The HTML for the grid
*/
const getGridHTML = () => {
let html = "";
randomlyOrderDinos();
dinos.forEach((dino) => {
dino.setFacts();
html += dino.getTileHTML();
});
return html;
};
// Creates the human object
const human = new Animal("human");
// Listens to clicks in the form button
formSubmit.addEventListener("click", () => {
const form = document.getElementById("dino-compare");
const grid = document.getElementById("grid");
const name = document.getElementById("name").value;
const feet = parseFloat(document.getElementById("feet").value);
const inches = parseFloat(document.getElementById("inches").value);
const weight = parseFloat(document.getElementById("weight").value);
const diet = document.getElementById("diet").value.toLowerCase();
addPropsToHuman(name, feet, inches, weight, diet);
form.className = "hidden";
grid.innerHTML = getGridHTML();
});
})();
});