DhruvDecoder commited on
Commit
7b1266a
·
verified ·
1 Parent(s): d02ca82

Create script.js

Browse files
Files changed (1) hide show
  1. script.js +263 -0
script.js ADDED
@@ -0,0 +1,263 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const animeData = {
2
+ "Naruto": { "genres": ["Action", "Adventure", "Fantasy"] },
3
+ "Death Note": { "genres": ["Mystery", "Psychological", "Supernatural", "Thriller"] },
4
+ "Attack on Titan": { "genres": ["Action", "Dark Fantasy", "Post-apocalyptic"] },
5
+ "One Punch Man": { "genres": ["Action", "Comedy", "Superhero"] },
6
+ "My Hero Academia": { "genres": ["Superhero", "Action", "Comedy"] },
7
+ "Fullmetal Alchemist: Brotherhood": { "genres": ["Adventure", "Dark Fantasy", "Steampunk"] },
8
+ "Demon Slayer": { "genres": ["Adventure", "Dark Fantasy", "Martial Arts"] },
9
+ "Steins;Gate": { "genres": ["Sci-Fi", "Thriller", "Time Travel"] },
10
+ "Hunter x Hunter": { "genres": ["Adventure", "Fantasy", "Martial Arts"] },
11
+ "Code Geass": { "genres": ["Mecha", "Alternate History", "Military"] },
12
+ "Neon Genesis Evangelion": { "genres": ["Psychological", "Science Fiction", "Mecha"] },
13
+ "Cowboy Bebop": { "genres": ["Space Western", "Science Fiction", "Neo-noir"] },
14
+ "Gintama": { "genres": ["Action", "Comedy", "Science Fiction"] },
15
+ "One Piece": { "genres": ["Adventure", "Fantasy", "Comedy"] },
16
+ "Sword Art Online": { "genres": ["Science Fiction", "Adventure", "Romance"] }
17
+ };
18
+
19
+ const animeQuotes = [
20
+ { quote: "I'll take a potato chip... and eat it!", author: "Light Yagami", anime: "Death Note" },
21
+ { quote: "The only ones who should kill are those who are prepared to be killed.", author: "Lelouch Lamperouge", anime: "Code Geass" },
22
+ { quote: "People die if they are killed.", author: "Shirou Emiya", anime: "Fate/stay night" },
23
+ { quote: "I don't want to conquer anything. I just think the guy with the most freedom in this whole ocean... that's the Pirate King!", author: "Monkey D. Luffy", anime: "One Piece" },
24
+ { quote: "The world isn't perfect. But it's there for us, doing the best it can... that's what makes it so damn beautiful.", author: "Roy Mustang", anime: "Fullmetal Alchemist" },
25
+ { quote: "If you don't take risks, you can't create a future!", author: "Monkey D. Luffy", anime: "One Piece" },
26
+ { quote: "Whatever you lose, you'll find it again. But what you throw away you'll never get back.", author: "Himura Kenshin", anime: "Rurouni Kenshin" },
27
+ { quote: "Fear is not evil. It tells you what your weakness is. And once you know your weakness, you can become stronger as well as kinder.", author: "Gildarts Clive", anime: "Fairy Tail" },
28
+ { quote: "It's not the face that makes someone a monster; it's the choices they make with their lives.", author: "Naruto Uzumaki", anime: "Naruto" },
29
+ { quote: "If you don't like your destiny, don't accept it. Instead, have the courage to change it the way you want it to be.", author: "Naruto Uzumaki", anime: "Naruto" }
30
+ ];
31
+
32
+ const quizQuestions = [
33
+ {
34
+ question: "How would you describe your personality?",
35
+ options: ["Energetic and optimistic", "Calm and analytical", "Determined and passionate", "Mysterious and aloof"]
36
+ },
37
+ {
38
+ question: "What's your ideal way to spend a weekend?",
39
+ options: ["Training and improving my skills", "Solving complex puzzles or mysteries", "Hanging out with friends", "Exploring new places"]
40
+ },
41
+ {
42
+ question: "If you had a superpower, what would it be?",
43
+ options: ["Super strength", "Mind reading", "Elemental control", "Time manipulation"]
44
+ },
45
+ {
46
+ question: "How do you handle conflicts?",
47
+ options: ["Face them head-on", "Analyze and strategize", "Try to understand all perspectives", "Avoid them if possible"]
48
+ },
49
+ {
50
+ question: "What's your biggest motivation in life?",
51
+ options: ["Protecting loved ones", "Seeking knowledge", "Achieving personal goals", "Making the world a better place"]
52
+ },
53
+ {
54
+ question: "Which animal best represents you?",
55
+ options: ["Fox", "Owl", "Lion", "Wolf"]
56
+ },
57
+ {
58
+ question: "What's your preferred fighting style?",
59
+ options: ["Hand-to-hand combat", "Strategic planning", "Weapons mastery", "Special abilities or magic"]
60
+ }
61
+ ];
62
+
63
+ const characters = [
64
+ { name: "Naruto Uzumaki", anime: "Naruto" },
65
+ { name: "L Lawliet", anime: "Death Note" },
66
+ { name: "Mikasa Ackerman", anime: "Attack on Titan" },
67
+ { name: "Spike Spiegel", anime: "Cowboy Bebop" },
68
+ { name: "Gon Freecss", anime: "Hunter x Hunter" },
69
+ { name: "Lelouch Lamperouge", anime: "Code Geass" },
70
+ { name: "Monkey D. Luffy", anime: "One Piece" },
71
+ { name: "Saitama", anime: "One Punch Man" }
72
+ ];
73
+
74
+ // DOM Elements
75
+ const animeListElement = document.getElementById('animeList');
76
+ const genreListElement = document.getElementById('genreList');
77
+ const recommendBtn = document.getElementById('recommendBtn');
78
+ const randomBtn = document.getElementById('randomBtn');
79
+ const recommendationsElement = document.getElementById('recommendations');
80
+ const quoteText = document.getElementById('quoteText');
81
+ const quoteAuthor = document.getElementById('quoteAuthor');
82
+ const newQuoteBtn = document.getElementById('newQuoteBtn');
83
+ const quizQuestion = document.getElementById('quizQuestion');
84
+ const quizOptions = document.getElementById('quizOptions');
85
+ const quizResult = document.getElementById('quizResult');
86
+ const restartQuizBtn = document.getElementById('restartQuizBtn');
87
+ const quizContainer = document.getElementById('quizContainer');
88
+
89
+ // State management
90
+ let selectedAnime = new Set();
91
+ let selectedGenres = new Set();
92
+ let currentQuestion = 0;
93
+ let userAnswers = [];
94
+
95
+ // Utility Functions
96
+ function createItem(text, listType) {
97
+ const item = document.createElement('div');
98
+ item.classList.add(`${listType}-item`);
99
+ item.textContent = text;
100
+ item.addEventListener('click', () => toggleItem(item, text, listType));
101
+ return item;
102
+ }
103
+
104
+ function toggleItem(item, text, listType) {
105
+ const selectedSet = listType === 'anime' ? selectedAnime : selectedGenres;
106
+ if (selectedSet.has(text)) {
107
+ selectedSet.delete(text);
108
+ item.classList.remove('selected');
109
+ } else {
110
+ selectedSet.add(text);
111
+ item.classList.add('selected');
112
+ }
113
+ }
114
+
115
+ // Recommendation System
116
+ function calculateRecommendations() {
117
+ if (selectedAnime.size === 0 && selectedGenres.size === 0) {
118
+ alert('Please select at least one anime or genre!');
119
+ return [];
120
+ }
121
+
122
+ const genreWeights = new Map();
123
+
124
+ // Weight genres from selected anime
125
+ selectedAnime.forEach(anime => {
126
+ const animeGenres = animeData[anime].genres;
127
+ animeGenres.forEach(genre => {
128
+ genreWeights.set(genre, (genreWeights.get(genre) || 0) + 1);
129
+ });
130
+ });
131
+
132
+ // Add weights from explicitly selected genres
133
+ selectedGenres.forEach(genre => {
134
+ genreWeights.set(genre, (genreWeights.get(genre) || 0) + 2);
135
+ });
136
+
137
+ // Calculate scores for each anime
138
+ const recommendations = [];
139
+ for (const [anime, data] of Object.entries(animeData)) {
140
+ if (selectedAnime.has(anime)) continue;
141
+
142
+ const score = data.genres.reduce((sum, genre) =>
143
+ sum + (genreWeights.get(genre) || 0), 0);
144
+
145
+ if (score > 0) {
146
+ recommendations.push({ title: anime, genres: data.genres, score });
147
+ }
148
+ }
149
+
150
+ return recommendations
151
+ .sort((a, b) => b.score - a.score)
152
+ .slice(0, 5)
153
+ .map(({ title, genres }) => ({ title, genres }));
154
+ }
155
+
156
+ function displayRecommendations(recommendations) {
157
+ recommendationsElement.innerHTML = '<h3>Recommendations:</h3>';
158
+ if (recommendations.length === 0) {
159
+ recommendationsElement.innerHTML += '<p>No recommendations found.</p>';
160
+ return;
161
+ }
162
+
163
+ const list = document.createElement('ul');
164
+ recommendations.forEach(anime => {
165
+ const item = document.createElement('li');
166
+ item.textContent = `${anime.title} - ${anime.genres.join(', ')}`;
167
+ list.appendChild(item);
168
+ });
169
+ recommendationsElement.appendChild(list);
170
+ }
171
+
172
+ // Quote System
173
+ function displayRandomQuote() {
174
+ const randomQuote = animeQuotes[Math.floor(Math.random() * animeQuotes.length)];
175
+ quoteText.textContent = `"${randomQuote.quote}"`;
176
+ quoteAuthor.textContent = `- ${randomQuote.author} (${randomQuote.anime})`;
177
+ }
178
+
179
+ // Quiz System
180
+ function displayQuestion() {
181
+ const question = quizQuestions[currentQuestion];
182
+ quizQuestion.textContent = question.question;
183
+ quizOptions.innerHTML = '';
184
+ question.options.forEach((option, index) => {
185
+ const button = document.createElement('button');
186
+ button.textContent = option;
187
+ button.addEventListener('click', () => selectAnswer(index));
188
+ quizOptions.appendChild(button);
189
+ });
190
+ }
191
+
192
+ function selectAnswer(index) {
193
+ userAnswers.push(index);
194
+ if (currentQuestion < quizQuestions.length - 1) {
195
+ currentQuestion++;
196
+ displayQuestion();
197
+ } else {
198
+ showResult();
199
+ }
200
+ }
201
+
202
+ function showResult() {
203
+ const characterIndex = Math.floor(Math.random() * characters.length);
204
+ const character = characters[characterIndex];
205
+ quizContainer.style.display = 'none';
206
+ quizResult.style.display = 'block';
207
+ quizResult.innerHTML = `
208
+ <h3>Quiz Result</h3>
209
+ <p>Based on your answers, you're most like <strong>${character.name}</strong> from ${character.anime}!</p>
210
+ <p>Remember, this is just for fun. Every person is unique, just like every anime character!</p>
211
+ `;
212
+ restartQuizBtn.style.display = 'block';
213
+ }
214
+
215
+ function restartQuiz() {
216
+ currentQuestion = 0;
217
+ userAnswers = [];
218
+ quizContainer.style.display = 'block';
219
+ quizResult.style.display = 'none';
220
+ restartQuizBtn.style.display = 'none';
221
+ displayQuestion();
222
+ }
223
+
224
+ // Initialize UI
225
+ function initializeUI() {
226
+ // Initialize anime list
227
+ Object.keys(animeData).forEach(anime => {
228
+ animeListElement.appendChild(createItem(anime, 'anime'));
229
+ });
230
+
231
+ // Initialize genre list
232
+ const allGenres = new Set();
233
+ Object.values(animeData).forEach(data => {
234
+ data.genres.forEach(genre => allGenres.add(genre));
235
+ });
236
+ Array.from(allGenres).forEach(genre => {
237
+ genreListElement.appendChild(createItem(genre, 'genre'));
238
+ });
239
+
240
+ // Initialize quote
241
+ displayRandomQuote();
242
+
243
+ // Initialize quiz
244
+ displayQuestion();
245
+ }
246
+
247
+ // Event Listeners
248
+ recommendBtn.addEventListener('click', () => {
249
+ const recommendations = calculateRecommendations();
250
+ displayRecommendations(recommendations);
251
+ });
252
+
253
+ randomBtn.addEventListener('click', () => {
254
+ const allAnime = Object.keys(animeData);
255
+ const randomAnime = allAnime[Math.floor(Math.random() * allAnime.length)];
256
+ displayRecommendations([{ title: randomAnime, genres: animeData[randomAnime].genres }]);
257
+ });
258
+
259
+ newQuoteBtn.addEventListener('click', displayRandomQuote);
260
+ restartQuizBtn.addEventListener('click', restartQuiz);
261
+
262
+ // Initialize the application
263
+ document.addEventListener('DOMContentLoaded', initializeUI);