Skip to content

Commit c2cea2c

Browse files
Fix: Arrow navigation now works - ensure questions load before navigation
- Changed to async/await pattern for reliable question loading - Questions must be loaded from JSON before navigation works - Added fallback message if questions fail to load
1 parent d4a7c46 commit c2cea2c

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

docs/js/editor.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,22 @@ let startTime = null;
55
let questionsData = {}; // Will hold all 1000 questions
66

77
// Load questions data
8-
fetch('data/questions.json')
9-
.then(response => response.json())
10-
.then(data => {
11-
questionsData = data;
8+
async function initializeEditor() {
9+
try {
10+
const response = await fetch('data/questions.json');
11+
questionsData = await response.json();
1212
console.log('Loaded', Object.keys(questionsData).length, 'questions');
13-
})
14-
.catch(error => {
13+
14+
// Now load the first question
15+
loadQuestion(currentQuestion);
16+
} catch (error) {
1517
console.error('Error loading questions:', error);
1618
// Fallback to default
1719
questionsData = {};
18-
});
20+
// Still load the question with fallback data
21+
loadQuestion(currentQuestion);
22+
}
23+
}
1924

2025
document.addEventListener('DOMContentLoaded', function() {
2126
// Get level from URL parameter
@@ -24,8 +29,8 @@ document.addEventListener('DOMContentLoaded', function() {
2429
currentLevel = urlParams.get('level');
2530
}
2631

27-
// Load first question
28-
loadQuestion(currentQuestion);
32+
// Initialize editor and load questions
33+
initializeEditor();
2934

3035
// Event listeners
3136
document.getElementById('run-code').addEventListener('click', runCode);
@@ -175,6 +180,11 @@ function loadQuestion(questionNum) {
175180
</div>
176181
`;
177182
document.querySelector('.question-meta').innerHTML = metaHTML;
183+
} else {
184+
// Fallback if question data not loaded
185+
document.getElementById('question-title').textContent = `Practice Question ${questionNum}`;
186+
document.getElementById('question-description').innerHTML =
187+
`<p>Loading question details... If this persists, please refresh the page.</p>`;
178188
}
179189

180190
// Load saved code if exists

0 commit comments

Comments
 (0)