let editor; let currentLanguage = 'javascript'; let chart = null; let loadingInterval = null; let loadingMessageIndex = 0; const loadingMessages = [ "Parsing code structure...", "Analyzing algorithm patterns...", "Detecting time complexity...", "Identifying nested loops...", "Checking for recursion...", "Examining data structures...", "Loading execution environment...", "Compiling test cases...", "Generating test data...", "Running performance benchmarks...", "Measuring execution time...", "Testing with small inputs...", "Testing with large inputs...", "Analyzing growth patterns...", "Calculating complexity metrics...", "Fitting complexity curves...", "Validating results...", "Finalizing analysis..." ]; const irregularIntervals = [2400, 3800, 4500, 1000, 2900, 4300, 1100, 3400, 950, 2250, 850, 3200, 2050, 1200, 1400, 1800, 900, 1300]; require.config({ paths: { vs: 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.44.0/min/vs' } }); require(['vs/editor/editor.main'], function () { editor = monaco.editor.create(document.getElementById('editor'), { value: '// Write your algorithm here\nfunction algorithm(arr) {\n // Your code\n return arr;\n}', language: 'javascript', theme: 'vs-dark', fontSize: 14, minimap: { enabled: false }, scrollBeyondLastLine: false, automaticLayout: true, lineNumbers: 'on', roundedSelection: false, scrollbar: { vertical: 'visible', horizontal: 'visible' } }); setupEventListeners(); }); function setupEventListeners() { // Language selector document.getElementById('languageSelect').addEventListener('change', (e) => { currentLanguage = e.target.value; monaco.editor.setModelLanguage(editor.getModel(), currentLanguage); // default code template const templates = { javascript: '// Write your algorithm here\nfunction algorithm(arr) {\n // Your code\n return arr;\n}', python: '# Write your algorithm here\ndef algorithm(arr):\n # Your code\n return arr', go: '// Write your algorithm here\nfunc algorithm(arr []int) []int {\n // Your code\n return arr\n}' }; editor.setValue(templates[currentLanguage]); }); // Analyze button document.getElementById('analyzeBtn').addEventListener('click', analyzeCode); } function startDynamicLoading() { loadingMessageIndex = 0; const loadingText = document.querySelector('#loadingState p'); function updateMessage() { if (loadingMessageIndex < loadingMessages.length) { loadingText.textContent = loadingMessages[loadingMessageIndex]; if (loadingMessageIndex < loadingMessages.length - 1) { const nextInterval = irregularIntervals[loadingMessageIndex]; loadingMessageIndex++; loadingInterval = setTimeout(updateMessage, nextInterval); } else { loadingMessageIndex++; } } } updateMessage(); } function stopDynamicLoading() { if (loadingInterval) { clearTimeout(loadingInterval); loadingInterval = null; } } async function analyzeCode() { const code = editor.getValue(); if (!code.trim()) { alert('Please enter code to analyze'); return; } showLoading(); startDynamicLoading(); const analyzeBtn = document.getElementById('analyzeBtn'); analyzeBtn.disabled = true; try { const response = await fetch('/api/analyze', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ code: code, language: currentLanguage }) }); const result = await response.json(); if (!response.ok) { throw new Error(result.error || 'Analysis failed'); } displayResults(result); } catch (error) { displayError(error.message); } finally { stopDynamicLoading(); analyzeBtn.disabled = false; } } function showLoading() { document.getElementById('resultsContent').style.display = 'none'; document.getElementById('loadingState').classList.add('active'); } function displayResults(result) { stopDynamicLoading(); document.getElementById('loadingState').classList.remove('active'); document.getElementById('resultsContent').style.display = 'block'; const resultsHTML = `
Confidence: ${result.confidence.toFixed(1)}%