-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
155 lines (134 loc) · 5.2 KB
/
index.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
/*
This Christmas, you’ve been tasked with running an anagram quiz at
the family gathering.
You have been given a list of anagrams, but you suspect that some
of the anagram pairs might be incorrect.
Your job is to write a JavaScript function to loop through the array
and filter out any pairs that aren’t actually anagrams.
For this challenge, spaces will be ignored, so "Be The Helm" would
be considered a valid anagram of "Bethlehem".
*/
document.addEventListener('DOMContentLoaded', () => {
// Original anagrams array - balanced with 5 real and 5 false
const originalAnagrams = [
['Can Assault', 'Santa Claus'], // TRUE
['Refreshed Erudite Londoner', 'Rudolf the Red Nose Reindeer'], // TRUE
['Frosty The Snowman', 'Honesty Warms Front'], // FALSE
['Present Wraps', 'Paper Answers'], // FALSE
['Congress Liar', 'Carol Singers'], // TRUE
['Silent Night', 'Listen Night'], // TRUE
['Be The Helm', 'Betlehem'], // FALSE
['Is Car Thieves', 'Christmas Eve'], // FALSE
['Debit Card', 'Bad Credit'], // TRUE
['Merry Christmas', 'Happy Holidays'], // FALSE
];
let currentAnagrams = []; // Will hold our shuffled array
let currentPairIndex = 0;
let score = 0;
let totalAttempts = 0;
// DOM elements
const word1Element = document.getElementById('word1');
const word2Element = document.getElementById('word2');
const yesButton = document.getElementById('yes-btn');
const noButton = document.getElementById('no-btn');
const helpButton = document.getElementById('help-btn');
const tipElement = document.getElementById('tip');
const feedbackElement = document.getElementById('feedback');
const scoreElement = document.getElementById('score');
const attemptsElement = document.getElementById('attempts');
const currentQuestionElement = document.getElementById('current-question');
const gameContent = document.getElementById('game-content');
const gameOver = document.getElementById('game-over');
const finalScoreElement = document.getElementById('final-score');
const playAgainButton = document.getElementById('play-again');
// Fisher-Yates shuffle algorithm
function shuffleArray(array) {
const shuffled = [...array]; // Create a copy to shuffle
for (let i = shuffled.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
}
return shuffled;
}
function areAnagrams(str1, str2) {
const prepare = (s) =>
s.toLowerCase().replace(/\s+/g, '').split('').sort().join('');
return prepare(str1) === prepare(str2);
}
function displayCurrentPair() {
const currentPair = currentAnagrams[currentPairIndex];
word1Element.textContent = currentPair[0];
word2Element.textContent = currentPair[1];
currentQuestionElement.textContent = currentPairIndex + 1;
}
function showFeedback(isCorrect, actuallyIsAnagram) {
feedbackElement.textContent = isCorrect
? `Correct! ${
actuallyIsAnagram ? 'They are anagrams!' : 'They are not anagrams!'
}`
: `Wrong! ${
actuallyIsAnagram ? 'They are anagrams!' : 'They are not anagrams!'
}`;
feedbackElement.className = `feedback ${isCorrect ? 'success' : 'error'}`;
feedbackElement.classList.remove('hidden');
}
function updateStats() {
scoreElement.textContent = score;
attemptsElement.textContent = totalAttempts;
}
function handleGuess(isAnagramGuess) {
const currentPair = currentAnagrams[currentPairIndex];
const actuallyIsAnagram = areAnagrams(currentPair[0], currentPair[1]);
const isCorrect = isAnagramGuess === actuallyIsAnagram;
if (isCorrect) score++;
totalAttempts++;
showFeedback(isCorrect, actuallyIsAnagram);
updateStats();
// Disable buttons during feedback
yesButton.disabled = true;
noButton.disabled = true;
setTimeout(() => {
// Move to next pair if available
if (currentPairIndex < currentAnagrams.length - 1) {
currentPairIndex++;
feedbackElement.classList.add('hidden');
displayCurrentPair();
yesButton.disabled = false;
noButton.disabled = false;
} else {
endGame();
}
}, 2000);
}
function endGame() {
gameContent.classList.add('hidden');
gameOver.classList.remove('hidden');
finalScoreElement.textContent = score;
}
function resetGame() {
// Shuffle the original array to create a new game sequence
currentAnagrams = shuffleArray(originalAnagrams);
currentPairIndex = 0;
score = 0;
totalAttempts = 0;
updateStats();
displayCurrentPair();
feedbackElement.classList.add('hidden');
gameContent.classList.remove('hidden');
gameOver.classList.add('hidden');
yesButton.disabled = false;
noButton.disabled = false;
}
// Event listeners
yesButton.addEventListener('click', () => handleGuess(true));
noButton.addEventListener('click', () => handleGuess(false));
playAgainButton.addEventListener('click', resetGame);
helpButton.addEventListener('click', () => {
tipElement.classList.toggle('hidden');
helpButton.textContent = tipElement.classList.contains('hidden')
? 'Need Help?'
: 'Hide Help';
});
// Initialize first game
resetGame();
});