generated from codetracker-learning/INDIVIDUAL-PROJECT-sorting-hat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
252 lines (215 loc) · 8.48 KB
/
main.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
const students = [];
const voldysArmy = [];
const houses = ["Gryffindor", "Hufflepuff", "Ravenclaw", "Slytherin"];
// Global variable for knowing what page is currently active
let currentPage = "all";
// Render to DOM
const renderToDom = (divId, textToPrint) => {
const selectedDiv = document.querySelector(divId);
selectedDiv.innerHTML = textToPrint;
};
// Print the welcome banner
const welcomeBanner = () => {
const domString = `
<div class="jumbotron">
<h1 class="display-4">Welcome to Hogwarts!</h1>
<p class="lead">The finest school of witchcraft and wizardy <strong>in the world</strong>. Click to see where you belong!</p>
<p class="lead">
<button id="startBtn" class="btn btn-primary btn-lg" href="#" role="button">Try on the Sorting Hat</button>
</p>
<hr class="my-4">
</div>
`;
renderToDom("#welcome", domString);
};
// Print the student form
const studentForm = (event) => {
if (event.target.id === "startBtn") {
const domString = `
<form id="myForm">
<div class="form-group">
<label>Enter your name</label>
<input id="name" class="form-control" type="text" placeholder="Harry Potter">
</div>
<button id="sortBtn" type="submit" class="btn btn-primary">Sort</button>
</form>
`;
renderToDom("#form", domString);
}
};
// Display the house filter buttons
const buttons = () => {
const domString = `
<button type="button" class="btn btn-primary active" data-bs-toggle="button" autocomplete="off" aria-pressed="true" id="all">All Students</button>
<button type="button" class="btn btn-primary" data-bs-toggle="button" autocomplete="off" id="gryffindor">Gryffindor</button>
<button type="button" class="btn btn-primary" data-bs-toggle="button" autocomplete="off" id="hufflepuff">Hufflepuff</button>
<button type="button" class="btn btn-primary" data-bs-toggle="button" autocomplete="off" id="ravenclaw">Ravenclaw</button>
<button type="button" class="btn btn-primary" data-bs-toggle="button" autocomplete="off" id="slytherin">Slytherin</button>
`;
renderToDom("#buttonContainer", domString);
};
// Filters the students by house
const filterHouse = (array, house) => {
return array.filter(student => student.house.toLowerCase() === house);
};
// Prints the filtered houses
const handleFilterButtons = (event) => {
currentPage = event.target.id;
if (event.target.id === "all" && event.target.type === "button") {
studentCardBuilder(students);
} else if (event.target.type === "button") {
studentCardBuilder(filterHouse(students, event.target.id));
}
};
// Sort student into a random house
const studentSort = (event) => {
// Prevent browser from executing default action
event.preventDefault();
if (event.target.id === "sortBtn") {
const studentName = document.querySelector("#name").value;
if (studentName.length < 1) {
printWarning();
} else {
clearWarning();
buttons();
randomHouse = houseSort();
if (students.length === 0) {
studentId = 0;
} else {
studentId = students.length;
}
let houseCrest = "";
if(randomHouse === "Gryffindor") {
houseCrest = "https://i.pinimg.com/originals/93/85/bf/9385bf3ca546d3c750363a78a68e0c70.jpg";
} else if (randomHouse === "Hufflepuff") {
houseCrest = "https://cdn11.bigcommerce.com/s-ydriczk/images/stencil/1280x1280/products/88364/91134/Harry-Potter-Hufflepuff-Crest-Official-wall-mounted-cardboard-cutout-buy-now-at-star__21122.1507644096.jpg?c=2";
} else if (randomHouse === "Ravenclaw") {
houseCrest = "https://cdn11.bigcommerce.com/s-ydriczk/images/stencil/1280x1280/products/88363/91130/Harry-Potter-Ravenclaw-Crest-Official-wall-mounted-cardboard-cutout-buy-now-at-star__86173.1507642983.jpg?c=2";
} else {
houseCrest = "https://cdn11.bigcommerce.com/s-ydriczk/images/stencil/1280x1280/products/88362/91127/Harry-Potter-Slytherin-Crest-Official-wall-mounted-cardboard-cutout-buy-now-at-star__31920.1507640618.jpg?c=2";
}
const newStudent = {
name: studentName,
house: randomHouse,
id: studentId,
crest: houseCrest
}
students.push(newStudent);
sortByName(students);
studentCardBuilder(students);
}
}
};
// Print warning message if a name was not entered in the form
const printWarning = () => {
let domString = `<h6>Please type a name!</h6>`;
renderToDom("#warning", domString);
}
// Clear warning message
const clearWarning = () => {
let domString = `<h5></h5>`;
renderToDom("#warning", domString);
// Clear form after submitting
document.getElementById("myForm").reset();
}
// Randomizes a house to a student in the array
const houseSort = () => {
const house = houses[Math.floor(Math.random() * houses.length)];
const elem = document.getElementById("studentCard");
return house;
};
// Sorts the students by first name
const sortByName = (array) => {
array.sort((a,b) => {
let name1 = a.name.toLowerCase();
let name2 = b.name.toLowerCase();
if (name1 < name2) {
return -1;
}
if (name1 > name2) {
return 1;
}
return 0;
});
};
// Expels a student to Voldy's Army
const studentExpel = (event) => {
const targetId = event.target.id;
const targetType = event.target.type;
if (currentPage !== "all" && targetType === "button") { // Check if the houses have been filtered and the expel button was pressed
const tempArray = students.filter(student => student.house.toLowerCase() === currentPage); // Filter the houses again and assign it to a temporary array
let i = 0;
while (i < students.length) {
if (students[i] === tempArray[targetId]) { // Loop through the main array to compare the students in the filtered
tempArray.splice(targetId, 1); // Delete the student from the filtered aray
const expelledStudent = students.splice(i, 1); // Delete the student from the main array
voldysArmy.push(expelledStudent[0]); // Add the expelled student to Voldy's array
studentCardBuilder(tempArray); // Build the filtered array
sortByName(voldysArmy); // Sort cards by name
deathEaterCardBuilder(voldysArmy); // Build Voldy's array
break; // Break out of the loop;
}
i++;
};
} else if (targetType === "button") {
expelledStudent = students.splice(targetId, 1);
voldysArmy.push(expelledStudent[0]);
studentCardBuilder(students);
sortByName(voldysArmy);
deathEaterCardBuilder(voldysArmy);
}
};
// Updates a student card
const updateStudent = (event) => {
//create edit button
//event listener for edit button
//event handler for edit button
};
// Builds the Death Eater card
const deathEaterCardBuilder = (voldysArray) => {
let domString = "";
voldysArray.forEach((deathEater) => {
deathEater.house = "Death Eater"
domString += `
<div id="Death_Eater" class="card" style="width: 18rem;">
<img class="card-img-top" src="https://cdn.shopify.com/s/files/1/0030/6003/9729/products/il_fullxfull.1619712634_nn9z_452x.jpg?v=1556590059" alt="death eater mark">
<div class="card-body">
<h5 class="card-title">${deathEater.name}</h5>
<p class="card-text">${deathEater.house}</p>
</div>
</div>
`;
});
renderToDom("#voldyContainer", domString);
};
// Builds the student card
const studentCardBuilder = (studentsArray) => {
let domString = "";
studentsArray.forEach((student, i) => {
domString += `
<div id="${student.house}" class="card" style="width: 18rem;">
<img class="card-img-top" src="${student.crest}" alt="student house crest">
<div class="card-body">
<h5 class="card-title">${student.name}</h5>
<p class="card-text">${student.house}</p>
<button id="${i}" type="button" class="btn btn-primary expel">Expel</button>
</div>
</div>
`;
});
renderToDom("#cardContainer", domString);
};
// Handles the button events
const buttonEvents = () => {
document.querySelector("#welcome").addEventListener("click", studentForm);
document.querySelector("#form").addEventListener("click", studentSort);
document.querySelector("#buttonContainer").addEventListener("click", handleFilterButtons);
document.querySelector("#cardContainer").addEventListener("click", studentExpel);
// document.querySelector("#cardContainer").addEventListener("click", studentUpdate);
}
// Starts the app
const init = () => {
welcomeBanner();
buttonEvents();
};
init();