Spaces:
Running
Running
File size: 8,125 Bytes
1af3855 1222789 1af3855 1222789 1af3855 1222789 1af3855 1222789 1af3855 1222789 1af3855 1222789 1af3855 1222789 1af3855 1222789 449f607 1222789 449f607 1222789 449f607 1222789 449f607 1222789 449f607 1af3855 449f607 1222789 449f607 1222789 449f607 1222789 449f607 1222789 1af3855 1222789 | 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 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CodeTyper Racer - Challenge</title>
<!-- Tailwind -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- Feather Icons (ONLY ONCE β FIXED) -->
<script src="https://unpkg.com/feather-icons"></script>
</head>
<body class="bg-gray-900 text-gray-100 min-h-screen">
<!-- ================= HEADER ================= -->
<header class="bg-gray-800 py-4 shadow-lg">
<div class="container mx-auto px-4 flex justify-between items-center">
<h1 class="text-xl font-bold">CodeTyper Racer π</h1>
<div class="flex items-center space-x-4">
<div id="timerDisplay" class="bg-gray-700 px-4 py-2 rounded-lg font-mono">00:00</div>
<button id="leaderboardBtn" class="flex items-center space-x-1 bg-gray-700 px-3 py-2 rounded-lg hover:bg-gray-600">
<i data-feather="bar-chart-2"></i>
<span>Leaderboard</span>
</button>
</div>
</div>
</header>
<!-- ================= MAIN ================= -->
<main class="container mx-auto px-4 py-8">
<div class="flex flex-col lg:flex-row gap-8">
<!-- SETTINGS -->
<div class="lg:w-1/4 bg-gray-800 p-6 rounded-xl shadow-lg">
<h2 class="text-lg font-semibold mb-3">Language</h2>
<button data-language="html" class="language-btn w-full bg-blue-600 py-2 rounded mb-2">HTML</button>
<button data-language="css" class="language-btn w-full bg-blue-600 py-2 rounded mb-2">CSS</button>
<button data-language="javascript" class="language-btn w-full bg-blue-600 py-2 rounded">JavaScript</button>
<h2 class="text-lg font-semibold mt-6 mb-3">Timer</h2>
<button data-time="1" class="time-btn w-full bg-gray-700 py-2 rounded mb-2">1 min</button>
<button data-time="5" class="time-btn w-full bg-gray-700 py-2 rounded mb-2">5 min</button>
<button data-time="10" class="time-btn w-full bg-gray-700 py-2 rounded">10 min</button>
<h2 class="text-lg font-semibold mt-6 mb-3">Difficulty</h2>
<button data-difficulty="easy" class="difficulty-btn w-full bg-green-600 py-2 rounded mb-2">Easy</button>
<button data-difficulty="medium" class="difficulty-btn w-full bg-yellow-600 py-2 rounded mb-2">Medium</button>
<button data-difficulty="hard" class="difficulty-btn w-full bg-red-600 py-2 rounded">Hard</button>
<button id="startTestBtn" class="mt-6 w-full bg-purple-600 py-3 rounded font-bold">Start Test</button>
</div>
<!-- TYPING AREA -->
<div class="lg:w-3/4">
<div id="codeDisplay" class="bg-gray-800 p-6 rounded-xl font-mono h-64 overflow-y-auto mb-4">
<p class="italic text-gray-500">Select settings and start</p>
</div>
<textarea id="typingArea" disabled class="w-full h-48 bg-gray-800 p-4 rounded font-mono"></textarea>
<div class="h-2 bg-gray-700 rounded mt-2">
<div id="progressFill" class="h-full bg-blue-500 rounded" style="width:0%"></div>
</div>
<div class="flex justify-between mt-4">
<div id="wpmDisplay">WPM: 0</div>
<div id="accuracyDisplay">Accuracy: 0%</div>
<button id="submitTestBtn" disabled class="bg-green-600 px-4 py-2 rounded">Submit</button>
</div>
</div>
</div>
</main>
<!-- ================= SCRIPT ================= -->
<script>
feather.replace();
/* ---------- USER DATA ---------- */
if (!localStorage.getItem('userData')) {
localStorage.setItem('userData', JSON.stringify({
name: prompt("Name:") || "Anonymous",
gradeLevel: prompt("Grade:") || "-",
section: prompt("Section:") || "-"
}));
}
/* ---------- CODE SNIPPETS ---------- */
const codeSnippets = {
html:{easy:`<h1>Hello World</h1>`,medium:`<div><p>Sample</p></div>`,hard:`<!DOCTYPE html><html><body><h1>Hello</h1></body></html>`},
css:{easy:`body{margin:0}`,medium:`.box{padding:10px}`,hard:`@media(max-width:600px){body{background:red}}`},
javascript:{easy:`console.log("Hi")`,medium:`function add(a,b){return a+b}`,hard:`class App{constructor(){console.log("Start")}}`}
};
/* ---------- STATE ---------- */
let selectedLanguage="html",selectedTime=1,selectedDifficulty="easy";
let startTime,timer,testActive=false,currentCode="",correctChars=0;
/* ---------- DOM ---------- */
const codeDisplay=document.getElementById("codeDisplay");
const typingArea=document.getElementById("typingArea");
const timerDisplay=document.getElementById("timerDisplay");
const progressFill=document.getElementById("progressFill");
const wpmDisplay=document.getElementById("wpmDisplay");
const accuracyDisplay=document.getElementById("accuracyDisplay");
/* ---------- BUTTONS ---------- */
document.querySelectorAll(".language-btn").forEach(b =>
b.onclick = (e) => {
selectedLanguage = b.dataset.language;
setActiveButton(e.target, 'language');
}
);
document.querySelectorAll(".time-btn").forEach(b =>
b.onclick = (e) => {
selectedTime = +b.dataset.time;
setActiveButton(e.target, 'time');
}
);
document.querySelectorAll(".difficulty-btn").forEach(b =>
b.onclick = (e) => {
selectedDifficulty = b.dataset.difficulty;
setActiveButton(e.target, 'difficulty');
}
);
// Set first button in each category as active by default
document.querySelector('.language-btn').click();
document.querySelector('.time-btn').click();
document.querySelector('.difficulty-btn').click();
/* ---------- START ---------- */
document.getElementById("startTestBtn").onclick=()=>{
currentCode = codeSnippets[selectedLanguage][selectedDifficulty];
renderCodeDisplay();
typingArea.disabled = false;
typingArea.value = "";
typingArea.focus();
correctChars = 0;
progressFill.style.width = "0%";
wpmDisplay.textContent = "WPM: 0";
accuracyDisplay.textContent = "Accuracy: 0%";
startTime=Date.now();
testActive=true;
let sec=selectedTime*60;
timerDisplay.textContent=`00:${sec}`;
clearInterval(timer);
timer=setInterval(()=>{
sec--;
timerDisplay.textContent=`${Math.floor(sec/60).toString().padStart(2,"0")}:${(sec%60).toString().padStart(2,"0")}`;
if(sec<=0) endTest();
},1000);
};
/* ---------- TYPING ---------- */
function renderCodeDisplay() {
const typed = typingArea.value;
let html = '';
for (let i = 0; i < currentCode.length; i++) {
let char = currentCode[i];
let spanClass = '';
if (i < typed.length) {
spanClass = typed[i] === char ? 'correct' : 'incorrect';
} else if (i === typed.length) {
spanClass = 'current cursor';
}
char = char === '\n' ? '<br>' :
char === ' ' ? ' ' :
char === '<' ? '<' :
char === '>' ? '>' : char;
html += spanClass ? `<span class="${spanClass}">${char}</span>` : char;
}
codeDisplay.innerHTML = html;
}
typingArea.oninput = () => {
if (!testActive) return;
const typed = typingArea.value;
correctChars = 0;
for (let i = 0; i < typed.length; i++) {
if (typed[i] === currentCode[i]) correctChars++;
}
const acc = Math.round((correctChars / typed.length) * 100) || 0;
const wpm = Math.round((correctChars / 5) / ((Date.now() - startTime) / 60000)) || 0;
accuracyDisplay.textContent = `Accuracy: ${acc}%`;
wpmDisplay.textContent = `WPM: ${wpm}`;
progressFill.style.width = `${Math.min(typed.length / currentCode.length * 100, 100)}%`;
renderCodeDisplay();
if (typed.length >= currentCode.length) {
endTest();
}
};
/* ---------- END ---------- */
function endTest() {
if (!testActive) return;
testActive = false;
clearInterval(timer);
typingArea.disabled = true;
const endTime = Date.now();
const stats = calculateTypingStats(
startTime,
endTime,
correctChars,
currentCode.length
);
saveToLeaderboard(stats);
showResultsModal(stats);
}
</script>
</body>
</html>
|