Download script.js from ROSS09/codejudge-pro-competitive-programming-platform-1ggln: direct link, hf CLI and curl.
- Browser
- Download file 32 kB
-
https://huggingface.co/spaces/ROSS09/codejudge-pro-competitive-programming-platform-1ggln/resolve/main/script.js
- Command line
-
hf download hf://spaces/ROSS09/codejudge-pro-competitive-programming-platform-1ggln/script.js
-
curl -L -o script.js https://huggingface.co/spaces/ROSS09/codejudge-pro-competitive-programming-platform-1ggln/resolve/main/script.js
32 kB
| // CodeJudge Pro - Main Application Script | |
| document.addEventListener('DOMContentLoaded', function() { | |
| // Initialize variables | |
| let editor; | |
| let isDarkTheme = true; | |
| let currentLanguage = 'cpp'; | |
| let isJudging = false; | |
| let submissionQueue = []; | |
| let testCases = []; | |
| let mockResults = []; | |
| let userSubmissions = []; | |
| let customTestCases = []; | |
| let currentUser = null; | |
| // DOM Elements | |
| const runBtn = document.getElementById('runBtn'); | |
| const submitBtn = document.getElementById('submitBtn'); | |
| const resetBtn = document.getElementById('resetBtn'); | |
| const formatBtn = document.getElementById('formatBtn'); | |
| const themeToggle = document.getElementById('themeToggle'); | |
| const languageSelect = document.getElementById('languageSelect'); | |
| const lineCount = document.getElementById('lineCount'); | |
| const charCount = document.getElementById('charCount'); | |
| const executionStatus = document.getElementById('executionStatus'); | |
| const resultsContainer = document.getElementById('resultsContainer'); | |
| const consoleOutput = document.getElementById('consoleOutput'); | |
| const outputTabs = document.querySelectorAll('.output-tab'); | |
| const toast = document.getElementById('toast'); | |
| const closeToast = document.getElementById('closeToast'); | |
| const loadingOverlay = document.getElementById('loadingOverlay'); | |
| const tabButtons = document.querySelectorAll('.tab-btn'); | |
| const tabContents = document.querySelectorAll('.tab-content'); | |
| // Default C++ template code | |
| const cppTemplate = `#include <iostream> | |
| #include <vector> | |
| #include <unordered_map> | |
| using namespace std; | |
| class Solution { | |
| public: | |
| vector<int> twoSum(vector<int>& nums, int target) { | |
| unordered_map<int, int> map; | |
| for (int i = 0; i < nums.size(); i++) { | |
| int complement = target - nums[i]; | |
| if (map.find(complement) != map.end()) { | |
| return {map[complement], i}; | |
| } | |
| map[nums[i]] = i; | |
| } | |
| return {}; | |
| } | |
| }; | |
| int main() { | |
| Solution sol; | |
| // Example test case | |
| vector<int> nums = {2, 7, 11, 15}; | |
| int target = 9; | |
| vector<int> result = sol.twoSum(nums, target); | |
| // Output format required by judge | |
| cout << "["; | |
| for (int i = 0; i < result.size(); i++) { | |
| cout << result[i]; | |
| if (i < result.size() - 1) { | |
| cout << ","; | |
| } | |
| } | |
| cout << "]" << endl; | |
| return 0; | |
| }`; | |
| // Mock test cases for simulation | |
| const mockTestCases = [ | |
| { | |
| id: 1, | |
| input: "nums = [2,7,11,15]\ntarget = 9", | |
| expected: "[0,1]", | |
| isHidden: false, | |
| description: "Simple case with early match" | |
| }, | |
| { | |
| id: 2, | |
| input: "nums = [3,2,4]\ntarget = 6", | |
| expected: "[1,2]", | |
| isHidden: false, | |
| description: "Match not at beginning" | |
| }, | |
| { | |
| id: 3, | |
| input: "nums = [3,3]\ntarget = 6", | |
| expected: "[0,1]", | |
| isHidden: true, | |
| description: "Duplicate numbers" | |
| }, | |
| { | |
| id: 4, | |
| input: "nums = [1,2,3,4,5,6,7,8,9,10]\ntarget = 19", | |
| expected: "[8,9]", | |
| isHidden: true, | |
| description: "Large array, match at end" | |
| }, | |
| { | |
| id: 5, | |
| input: "nums = [-1,-2,-3,-4,-5]\ntarget = -8", | |
| expected: "[2,4]", | |
| isHidden: true, | |
| description: "Negative numbers" | |
| } | |
| ]; | |
| // Mock user submissions history | |
| const initialSubmissions = [ | |
| { | |
| id: 1, | |
| problem: "Two Sum", | |
| language: "C++", | |
| code: "vector<int> twoSum(vector<int>& nums, int target) {\n unordered_map<int, int> map;\n for (int i = 0; i < nums.size(); i++) {\n int complement = target - nums[i];\n if (map.find(complement) != map.end()) {\n return {map[complement], i};\n }\n map[nums[i]] = i;\n }\n return {};\n}", | |
| status: 'accepted', | |
| time: 12, | |
| memory: 8560, | |
| message: 'All test cases passed', | |
| timestamp: Date.now() - 2000, | |
| testResults: [ | |
| {caseId: 1, passed: true, time: 4, output: '[0,1]'}, | |
| {caseId: 2, passed: true, time: 3, output: '[1,2]'}, | |
| {caseId: 3, passed: true, time: 5, output: '[0,1]'}, | |
| {caseId: 4, passed: true, time: 7, output: '[8,9]'}, | |
| {caseId: 5, passed: true, time: 6, output: '[2,4]'} | |
| ] | |
| }, | |
| { | |
| id: 2, | |
| problem: "Two Sum", | |
| language: "C++", | |
| code: "vector<int> twoSum(vector<int>& nums, int target) {\n for (int i = 0; i < nums.size(); i++) {\n for (int j = i + 1; j < nums.size(); j++) {\n if (nums[i] + nums[j] == target) {\n return {i, j};\n }\n }\n }\n return {};\n}", | |
| status: 'accepted', | |
| time: 45, | |
| memory: 9340, | |
| message: 'All test cases passed (brute force)', | |
| timestamp: Date.now() - 300000, | |
| testResults: [ | |
| {caseId: 1, passed: true, time: 12, output: '[0,1]'}, | |
| {caseId: 2, passed: true, time: 15, output: '[1,2]'}, | |
| {caseId: 3, passed: true, time: 18, output: '[0,1]'}, | |
| {caseId: 4, passed: true, time: 25, output: '[8,9]'}, | |
| {caseId: 5, passed: true, time: 22, output: '[2,4]'} | |
| ] | |
| }, | |
| { | |
| id: 3, | |
| problem: "Two Sum", | |
| language: "C++", | |
| code: "vector<int> twoSum(vector<int>& nums, int target) {\n unordered_map<int, int> map;\n for (int i = 0; i < nums.size(); i++) {\n map[nums[i]] = i;\n }\n for (int i = 0; i < nums.size(); i++) {\n int complement = target - nums[i];\n if (map.find(complement) != map.end() && map[complement] != i) {\n return {i, map[complement]};\n }\n }\n return {};\n}", | |
| status: 'wrong_answer', | |
| time: 8, | |
| memory: 7520, | |
| message: 'Test case 3 failed: duplicate numbers', | |
| timestamp: Date.now() - 600000, | |
| testResults: [ | |
| {caseId: 1, passed: true, time: 3, output: '[0,1]'}, | |
| {caseId: 2, passed: true, time: 5, output: '[1,2]'}, | |
| {caseId: 3, passed: false, time: 2, output: '[0,0]'} | |
| ] | |
| } | |
| ]; | |
| // Mock submission results for simulation | |
| const mockSubmissionResults = [ | |
| { | |
| id: 1, | |
| status: 'accepted', | |
| time: 12, | |
| memory: 8560, | |
| message: 'All test cases passed', | |
| testResults: [ | |
| {caseId: 1, passed: true, time: 4, output: '[0,1]'}, | |
| {caseId: 2, passed: true, time: 3, output: '[1,2]'}, | |
| {caseId: 3, passed: true, time: 5, output: '[0,1]'}, | |
| {caseId: 4, passed: true, time: 7, output: '[8,9]'}, | |
| {caseId: 5, passed: true, time: 6, output: '[2,4]'} | |
| ] | |
| }, | |
| { | |
| id: 2, | |
| status: 'wrong_answer', | |
| time: 8, | |
| memory: 7520, | |
| message: 'Test case 2 failed', | |
| testResults: [ | |
| {caseId: 1, passed: true, time: 3, output: '[0,1]'}, | |
| {caseId: 2, passed: false, time: 5, output: '[0,2]'}, | |
| {caseId: 3, passed: false, time: 2, output: '[0,0]'} | |
| ] | |
| }, | |
| { | |
| id: 3, | |
| status: 'compilation_error', | |
| time: 0, | |
| memory: 0, | |
| message: "error: expected ';' before '}' token", | |
| testResults: [] | |
| }, | |
| { | |
| id: 4, | |
| status: 'runtime_error', | |
| time: 15, | |
| memory: 12560, | |
| message: 'Segmentation fault (core dumped)', | |
| testResults: [ | |
| {caseId: 1, passed: false, time: 15, output: ''} | |
| ] | |
| }, | |
| { | |
| id: 5, | |
| status: 'time_limit_exceeded', | |
| time: 2000, | |
| memory: 12400, | |
| message: 'Time limit exceeded on test case 3', | |
| testResults: [ | |
| {caseId: 1, passed: true, time: 5, output: '[0,1]'}, | |
| {caseId: 2, passed: true, time: 7, output: '[1,2]'}, | |
| {caseId: 3, passed: false, time: 2000, output: ''} | |
| ] | |
| } | |
| ]; | |
| // Initialize Monaco Editor | |
| function initializeEditor() { | |
| require.config({ paths: { vs: 'https://cdn.jsdelivr.net/npm/monaco-editor@0.34.0/min/vs' } }); | |
| require(['vs/editor/editor.main'], function() { | |
| editor = monaco.editor.create(document.getElementById('editorContainer'), { | |
| value: cppTemplate, | |
| language: 'cpp', | |
| theme: 'vs-dark', | |
| fontSize: 14, | |
| fontFamily: "'JetBrains Mono', monospace", | |
| lineNumbers: 'on', | |
| roundedSelection: false, | |
| scrollBeyondLastLine: false, | |
| readOnly: false, | |
| automaticLayout: true, | |
| minimap: { enabled: false }, | |
| wordWrap: 'on', | |
| wrappingIndent: 'indent', | |
| renderLineHighlight: 'all', | |
| cursorBlinking: 'smooth', | |
| cursorSmoothCaretAnimation: 'on', | |
| formatOnType: true, | |
| formatOnPaste: true, | |
| suggestOnTriggerCharacters: true, | |
| acceptSuggestionOnEnter: 'on', | |
| scrollbar: { | |
| vertical: 'visible', | |
| horizontal: 'visible', | |
| useShadows: false | |
| } | |
| }); | |
| // Update line and character counts | |
| updateEditorStats(); | |
| // Listen to editor changes | |
| editor.onDidChangeModelContent(function() { | |
| updateEditorStats(); | |
| }); | |
| }); | |
| } | |
| // Update editor line and character count | |
| function updateEditorStats() { | |
| if (!editor) return; | |
| const model = editor.getModel(); | |
| if (model) { | |
| const lineCountValue = model.getLineCount(); | |
| const charCountValue = model.getValueLength(); | |
| lineCount.textContent = `Line: ${lineCountValue}`; | |
| charCount.textContent = `Chars: ${charCountValue}`; | |
| } | |
| } | |
| // Show toast notification | |
| function showToast(type, title, message, duration = 4000) { | |
| const icon = toast.querySelector('#toastIcon'); | |
| const titleEl = toast.querySelector('#toastTitle'); | |
| const messageEl = toast.querySelector('#toastMessage'); | |
| // Set toast style based on type | |
| switch(type) { | |
| case 'success': | |
| icon.className = 'w-6 h-6 rounded-full mr-3 flex items-center justify-center bg-green-500'; | |
| icon.innerHTML = '<i class="fas fa-check text-white text-xs"></i>'; | |
| break; | |
| case 'error': | |
| icon.className = 'w-6 h-6 rounded-full mr-3 flex items-center justify-center bg-red-500'; | |
| icon.innerHTML = '<i class="fas fa-exclamation text-white text-xs"></i>'; | |
| break; | |
| case 'warning': | |
| icon.className = 'w-6 h-6 rounded-full mr-3 flex items-center justify-center bg-yellow-500'; | |
| icon.innerHTML = '<i class="fas fa-exclamation-triangle text-white text-xs"></i>'; | |
| break; | |
| case 'info': | |
| default: | |
| icon.className = 'w-6 h-6 rounded-full mr-3 flex items-center justify-center bg-blue-500'; | |
| icon.innerHTML = '<i class="fas fa-info text-white text-xs"></i>'; | |
| } | |
| titleEl.textContent = title; | |
| messageEl.textContent = message; | |
| // Show toast with animation | |
| toast.classList.remove('translate-y-full'); | |
| toast.classList.add('translate-y-0'); | |
| // Auto hide after duration | |
| setTimeout(() => { | |
| toast.classList.remove('translate-y-0'); | |
| toast.classList.add('translate-y-full'); | |
| }, duration); | |
| } | |
| // Simulate code execution | |
| function simulateExecution(isSubmit = false) { | |
| if (isJudging) { | |
| showToast('warning', 'Already Judging', 'Please wait for current submission to complete'); | |
| return; | |
| } | |
| isJudging = true; | |
| loadingOverlay.classList.remove('hidden'); | |
| // Get code from editor | |
| const code = editor.getValue(); | |
| // Reset results | |
| resultsContainer.innerHTML = ''; | |
| // Show execution status | |
| executionStatus.classList.remove('hidden'); | |
| const statusText = document.getElementById('statusText'); | |
| const statusIcon = document.getElementById('statusIcon'); | |
| const timer = document.getElementById('timer'); | |
| // Simulate compilation phase | |
| statusText.textContent = 'Compiling...'; | |
| statusIcon.className = 'w-4 h-4 rounded-full bg-yellow-500 animate-pulse'; | |
| let timeElapsed = 0; | |
| const timerInterval = setInterval(() => { | |
| timeElapsed += 0.1; | |
| timer.textContent = `${timeElapsed.toFixed(1)}s`; | |
| }, 100); | |
| // Simulate different phases | |
| setTimeout(() => { | |
| statusText.textContent = 'Running Test Cases...'; | |
| statusIcon.className = 'w-4 h-4 rounded-full bg-blue-500 animate-pulse'; | |
| setTimeout(() => { | |
| // Generate random result for simulation | |
| const resultIndex = isSubmit ? | |
| (Math.random() > 0.7 ? 0 : Math.floor(Math.random() * (mockSubmissionResults.length - 1)) + 1) : | |
| 0; // Always show success for Run button | |
| const result = mockSubmissionResults[resultIndex]; | |
| // Show final result | |
| statusText.textContent = getStatusDisplayName(result.status); | |
| statusIcon.className = `w-4 h-4 rounded-full ${getStatusColor(result.status)}`; | |
| statusIcon.classList.remove('animate-pulse'); | |
| clearInterval(timerInterval); | |
| timer.textContent = `${(timeElapsed + 0.5).toFixed(1)}s`; | |
| // Display results | |
| displayResults(result, isSubmit); | |
| // Show toast notification | |
| showResultToast(result.status, result.message); | |
| // Save submission if it's a submit action | |
| if (isSubmit) { | |
| saveUserSubmission(result); | |
| } | |
| // Hide loading overlay | |
| setTimeout(() => { | |
| loadingOverlay.classList.add('hidden'); | |
| isJudging = false; | |
| }, 500); | |
| }, 1500); | |
| }, 1000); | |
| } | |
| // Display execution results | |
| function displayResults(result, showAllTests = false) { | |
| resultsContainer.innerHTML = ''; | |
| // Result header | |
| const resultHeader = document.createElement('div'); | |
| resultHeader.className = `flex items-center justify-between p-4 rounded-lg border ${getStatusBgClass(result.status)}`; | |
| resultHeader.innerHTML = ` | |
| <div class="flex items-center space-x-3"> | |
| <div class="w-4 h-4 rounded-full ${getStatusColor(result.status)}"></div> | |
| <div> | |
| <h4 class="font-bold">${getStatusDisplayName(result.status)}</h4> | |
| <p class="text-sm text-gray-400">${result.message}</p> | |
| </div> | |
| </div> | |
| <div class="text-right"> | |
| <div class="text-sm text-gray-300">${result.time} ms</div> | |
| <div class="text-sm text-gray-400">${result.memory} KB</div> | |
| </div> | |
| `; | |
| resultsContainer.appendChild(resultHeader); | |
| // Test case results (show only first 2 for run, all for submit) | |
| const testsToShow = showAllTests ? result.testResults : result.testResults.slice(0, 2); | |
| if (testsToShow.length > 0) { | |
| const testsHeader = document.createElement('div'); | |
| testsHeader.className = 'mt-4 mb-2'; | |
| testsHeader.innerHTML = `<h5 class="font-medium">Test Cases ${showAllTests ? '' : '(Sample)'}</h5>`; | |
| resultsContainer.appendChild(testsHeader); | |
| testsToShow.forEach(test => { | |
| const testElement = document.createElement('div'); | |
| testElement.className = `test-case-card p-4 rounded-lg border mb-3 ${test.passed ? 'border-green-800/50 bg-green-900/20' : 'border-red-800/50 bg-red-900/20'}`; | |
| testElement.innerHTML = ` | |
| <div class="flex items-center justify-between mb-2"> | |
| <div class="flex items-center space-x-2"> | |
| <div class="w-3 h-3 rounded-full ${test.passed ? 'bg-green-500' : 'bg-red-500'}"></div> | |
| <span class="font-medium">Test Case ${test.caseId}</span> | |
| </div> | |
| <div class="text-sm text-gray-400">${test.time} ms</div> | |
| </div> | |
| ${!test.passed ? ` | |
| <div class="mt-3 pt-3 border-t border-gray-800"> | |
| <div class="text-sm text-gray-400 mb-1">Output:</div> | |
| <div class="bg-gray-900 p-2 rounded font-mono text-sm">${test.output || 'No output'}</div> | |
| <div class="text-sm text-gray-400 mt-2 mb-1">Expected:</div> | |
| <div class="bg-gray-900 p-2 rounded font-mono text-sm">${mockTestCases.find(tc => tc.id === test.caseId)?.expected || 'N/A'}</div> | |
| </div> | |
| ` : ''} | |
| `; | |
| resultsContainer.appendChild(testElement); | |
| }); | |
| if (!showAllTests && result.testResults.length > 2) { | |
| const moreTests = document.createElement('div'); | |
| moreTests.className = 'text-center text-sm text-gray-500 py-2'; | |
| moreTests.textContent = `+ ${result.testResults.length - 2} more test cases hidden`; | |
| resultsContainer.appendChild(moreTests); | |
| } | |
| } | |
| // Add stats if accepted | |
| if (result.status === 'accepted') { | |
| const stats = document.createElement('div'); | |
| stats.className = 'mt-4 p-4 bg-gray-800/30 rounded-lg'; | |
| const percentile = Math.floor(Math.random() * 20) + 70; // Random percentile between 70-90 | |
| stats.innerHTML = ` | |
| <h5 class="font-medium mb-2">Performance</h5> | |
| <div class="grid grid-cols-2 gap-4"> | |
| <div> | |
| <div class="text-sm text-gray-400">Time</div> | |
| <div class="text-lg font-bold ${result.time < 50 ? 'text-green-400' : result.time < 100 ? 'text-yellow-400' : 'text-red-400'}">${result.time} ms</div> | |
| <div class="text-xs text-gray-500">Beats ${percentile}% of submissions</div> | |
| </div> | |
| <div> | |
| <div class="text-sm text-gray-400">Memory</div> | |
| <div class="text-lg font-bold ${result.memory < 10000 ? 'text-green-400' : result.memory < 20000 ? 'text-yellow-400' : 'text-red-400'}">${(result.memory / 1024).toFixed(1)} MB</div> | |
| <div class="text-xs text-gray-500">Beats ${percentile - 10}% of submissions</div> | |
| </div> | |
| </div> | |
| `; | |
| resultsContainer.appendChild(stats); | |
| } | |
| } | |
| // Get status display name | |
| function getStatusDisplayName(status) { | |
| const statusMap = { | |
| 'accepted': 'Accepted', | |
| 'wrong_answer': 'Wrong Answer', | |
| 'compilation_error': 'Compilation Error', | |
| 'runtime_error': 'Runtime Error', | |
| 'time_limit_exceeded': 'Time Limit Exceeded', | |
| 'memory_limit_exceeded': 'Memory Limit Exceeded' | |
| }; | |
| return statusMap[status] || status; | |
| } | |
| // Get status color class | |
| function getStatusColor(status) { | |
| const colorMap = { | |
| 'accepted': 'bg-green-500', | |
| 'wrong_answer': 'bg-red-500', | |
| 'compilation_error': 'bg-yellow-500', | |
| 'runtime_error': 'bg-orange-500', | |
| 'time_limit_exceeded': 'bg-purple-500', | |
| 'memory_limit_exceeded': 'bg-indigo-500' | |
| }; | |
| return colorMap[status] || 'bg-gray-500'; | |
| } | |
| // Get status background class | |
| function getStatusBgClass(status) { | |
| const bgMap = { | |
| 'accepted': 'border-green-800/50 bg-green-900/20', | |
| 'wrong_answer': 'border-red-800/50 bg-red-900/20', | |
| 'compilation_error': 'border-yellow-800/50 bg-yellow-900/20', | |
| 'runtime_error': 'border-orange-800/50 bg-orange-900/20', | |
| 'time_limit_exceeded': 'border-purple-800/50 bg-purple-900/20', | |
| 'memory_limit_exceeded': 'border-indigo-800/50 bg-indigo-900/20' | |
| }; | |
| return bgMap[status] || 'border-gray-800/50 bg-gray-900/20'; | |
| } | |
| // Get status text color class | |
| function getStatusTextColor(status) { | |
| const colorMap = { | |
| 'accepted': 'text-green-400', | |
| 'wrong_answer': 'text-red-400', | |
| 'compilation_error': 'text-yellow-400', | |
| 'runtime_error': 'text-orange-400', | |
| 'time_limit_exceeded': 'text-purple-400', | |
| 'memory_limit_exceeded': 'text-indigo-400' | |
| }; | |
| return colorMap[status] || 'text-gray-400'; | |
| } | |
| // Show result toast | |
| function showResultToast(status, message) { | |
| const typeMap = { | |
| 'accepted': 'success', | |
| 'wrong_answer': 'error', | |
| 'compilation_error': 'warning', | |
| 'runtime_error': 'error', | |
| 'time_limit_exceeded': 'warning', | |
| 'memory_limit_exceeded': 'warning' | |
| }; | |
| showToast(typeMap[status] || 'info', getStatusDisplayName(status), message); | |
| } | |
| // Format code (simplified) | |
| function formatCode() { | |
| if (!editor) return; | |
| const code = editor.getValue(); | |
| // Simple formatting simulation | |
| let formatted = code | |
| .replace(/\t/g, ' ') | |
| .replace(/\n\s*\n\s*\n/g, '\n\n'); | |
| editor.setValue(formatted); | |
| showToast('success', 'Code Formatted', 'Your code has been formatted successfully'); | |
| } | |
| // Toggle theme | |
| function toggleTheme() { | |
| isDarkTheme = !isDarkTheme; | |
| if (editor) { | |
| monaco.editor.setTheme(isDarkTheme ? 'vs-dark' : 'vs'); | |
| } | |
| themeToggle.innerHTML = isDarkTheme ? | |
| '<i class="fas fa-moon mr-1"></i>Dark' : | |
| '<i class="fas fa-sun mr-1"></i>Light'; | |
| document.body.classList.toggle('light-theme', !isDarkTheme); | |
| showToast('info', 'Theme Changed', `Switched to ${isDarkTheme ? 'Dark' : 'Light'} theme`); | |
| } | |
| // Reset editor to template | |
| function resetEditor() { | |
| if (!editor) return; | |
| const language = languageSelect.value; | |
| let template = cppTemplate; | |
| if (language === 'python') { | |
| template = `class Solution: | |
| def twoSum(self, nums, target): | |
| num_map = {} | |
| for i, num in enumerate(nums): | |
| complement = target - num | |
| if complement in num_map: | |
| return [num_map[complement], i] | |
| num_map[num] = i | |
| return [] | |
| if __name__ == "__main__": | |
| sol = Solution() | |
| nums = [2, 7, 11, 15] | |
| target = 9 | |
| result = sol.twoSum(nums, target) | |
| print(result)`; | |
| } else if (language === 'java') { | |
| template = `import java.util.*; | |
| class Solution { | |
| public int[] twoSum(int[] nums, int target) { | |
| Map<Integer, Integer> map = new HashMap<>(); | |
| for (int i = 0; i < nums.length; i++) { | |
| int complement = target - nums[i]; | |
| if (map.containsKey(complement)) { | |
| return new int[] { map.get(complement), i }; | |
| } | |
| map.put(nums[i], i); | |
| } | |
| return new int[0]; | |
| } | |
| public static void main(String[] args) { | |
| Solution sol = new Solution(); | |
| int[] nums = {2, 7, 11, 15}; | |
| int target = 9; | |
| int[] result = sol.twoSum(nums, target); | |
| System.out.println(Arrays.toString(result)); | |
| } | |
| }`; | |
| } | |
| editor.setValue(template); | |
| showToast('info', 'Code Reset', 'Editor has been reset to default template'); | |
| } | |
| // Change language | |
| function changeLanguage() { | |
| const language = languageSelect.value; | |
| currentLanguage = language; | |
| if (editor) { | |
| monaco.editor.setModelLanguage(editor.getModel(), language); | |
| } | |
| resetEditor(); | |
| showToast('info', 'Language Changed', `Switched to ${languageSelect.options[languageSelect.selectedIndex].text}`); | |
| } | |
| // Tab switching | |
| function setupTabs() { | |
| tabButtons.forEach(button => { | |
| button.addEventListener('click', function() { | |
| const tabId = this.getAttribute('data-tab'); | |
| // Update active tab button | |
| tabButtons.forEach(btn => { | |
| btn.classList.remove('active', 'border-blue-500', 'text-blue-400'); | |
| btn.classList.add('text-gray-400'); | |
| }); | |
| this.classList.add('active', 'border-blue-500', 'text-blue-400'); | |
| this.classList.remove('text-gray-400'); | |
| // Show active tab content | |
| tabContents.forEach(content => { | |
| content.classList.remove('active'); | |
| content.classList.add('hidden'); | |
| }); | |
| document.getElementById(tabId).classList.add('active'); | |
| document.getElementById(tabId).classList.remove('hidden'); | |
| }); | |
| }); | |
| } | |
| // Output tab switching | |
| function setupOutputTabs() { | |
| outputTabs.forEach(tab => { | |
| tab.addEventListener('click', function() { | |
| const tabId = this.getAttribute('data-tab'); | |
| // Update active output tab | |
| outputTabs.forEach(t => { | |
| t.classList.remove('active', 'border-blue-500', 'text-blue-400'); | |
| t.classList.add('text-gray-400'); | |
| }); | |
| this.classList.add('active', 'border-blue-500', 'text-blue-400'); | |
| this.classList.remove('text-gray-400'); | |
| // Show console or output | |
| if (tabId === 'console') { | |
| consoleOutput.classList.remove('hidden'); | |
| } else { | |
| consoleOutput.classList.add('hidden'); | |
| } | |
| }); | |
| }); | |
| } | |
| // Initialize everything | |
| function init() { | |
| initializeEditor(); | |
| setupTabs(); | |
| setupOutputTabs(); | |
| // Event listeners | |
| runBtn.addEventListener('click', () => simulateExecution(false)); | |
| submitBtn.addEventListener('click', () => simulateExecution(true)); | |
| resetBtn.addEventListener('click', resetEditor); | |
| formatBtn.addEventListener('click', formatCode); | |
| themeToggle.addEventListener('click', toggleTheme); | |
| languageSelect.addEventListener('change', changeLanguage); | |
| closeToast.addEventListener('click', () => { | |
| toast.classList.remove('translate-y-0'); | |
| toast.classList.add('translate-y-full'); | |
| }); | |
| // New event listeners for navigation and modals | |
| document.getElementById('problemsLink').addEventListener('click', (e) => { | |
| e.preventDefault(); | |
| showToast('info', 'Problems', 'Browse all coding problems - coming soon!'); | |
| }); | |
| document.getElementById('contestsLink').addEventListener('click', (e) => { | |
| e.preventDefault(); | |
| showToast('info', 'Contests', 'Participate in coding contests - coming soon!'); | |
| }); | |
| document.getElementById('leaderboardLink').addEventListener('click', (e) => { | |
| e.preventDefault(); | |
| showToast('info', 'Leaderboard', 'View global rankings - coming soon!'); | |
| }); | |
| document.getElementById('discussLink').addEventListener('click', (e) => { | |
| e.preventDefault(); | |
| showToast('info', 'Discuss', 'Community discussions - coming soon!'); | |
| }); | |
| document.querySelector('.bg-gradient-to-r.from-blue-600').addEventListener('click', (e) => { | |
| e.preventDefault(); | |
| showToast('info', 'Sign In', 'Authentication system - coming soon!'); | |
| }); | |
| // Test case modal | |
| document.getElementById('addTestBtn').addEventListener('click', () => { | |
| document.getElementById('testCaseModal').classList.remove('hidden'); | |
| }); | |
| document.getElementById('closeTestCaseModal').addEventListener('click', () => { | |
| document.getElementById('testCaseModal').classList.add('hidden'); | |
| }); | |
| document.getElementById('cancelTestCase').addEventListener('click', () => { | |
| document.getElementById('testCaseModal').classList.add('hidden'); | |
| }); | |
| document.getElementById('saveTestCase').addEventListener('click', saveCustomTestCase); | |
| // Submission modal | |
| document.getElementById('closeSubmissionModal').addEventListener('click', () => { | |
| document.getElementById('submissionModal').classList.add('hidden'); | |
| }); | |
| // Submission items click handlers | |
| document.querySelectorAll('.submission-item').forEach(item => { | |
| item.addEventListener('click', () => { | |
| const submissionId = item.getAttribute('data-submission'); | |
| showSubmissionDetails(submissionId); | |
| }); | |
| }); | |
| // Keyboard shortcuts | |
| document.addEventListener('keydown', function(e) { | |
| // Ctrl+Enter to submit | |
| if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { | |
| e.preventDefault(); | |
| simulateExecution(true); | |
| } | |
| // Ctrl+R to run | |
| if ((e.ctrlKey || e.metaKey) && e.key === 'r' && !e.shiftKey) { | |
| e.preventDefault(); | |
| simulateExecution(false); | |
| } | |
| // Ctrl+Shift+F to format | |
| if ((e.ctrlKey || e.metaKey) && e.shiftKey && e.key === 'F') { | |
| e.preventDefault(); | |
| formatCode(); | |
| } | |
| }); | |
| // Initial toast | |
| setTimeout(() => { | |
| showToast('info', 'Welcome to CodeJudge Pro', 'Start by writing your solution and clicking "Run Code" to test it'); | |
| }, 1000); | |
| } | |
| // Start the application | |
| init(); | |
| // Additional enhancements will be loaded from script-fixes.js | |
| // This ensures backward compatibility while adding new features | |
| }); |