-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
60 lines (53 loc) · 1.88 KB
/
script.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
let editor;
window.onload = function() {
editor = CodeMirror.fromTextArea(document.getElementById("code-editor"), {
lineNumbers: true,
mode: "javascript"
});
fetchQuestions();
};
function fetchQuestions() {
fetch('http://localhost:3000/questions')
.then(response => response.json())
.then(data => {
window.questions = data;
const sidebar = document.querySelector('.sidebar ul');
sidebar.innerHTML = '';
data.forEach((question, index) => {
const li = document.createElement('li');
li.textContent = question.title;
li.dataset.index = index;
li.onclick = () => loadQuestion(index);
sidebar.appendChild(li);
});
});
}
function loadQuestion(index) {
const question = window.questions[index];
const questionTitleElement = document.getElementById("question-title");
questionTitleElement.innerText = question.title;
questionTitleElement.dataset.index = index;
document.getElementById("question-description").innerText = question.description;
editor.setValue(question.functionSignature);
}
function submitCode() {
const userCode = editor.getValue();
const questionIndex = document.getElementById("question-title").dataset.index;
const questionId = window.questions[questionIndex]._id;
console.log('Submitting code for question:', questionId);
fetch('http://localhost:3000/submit', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ questionId, userCode })
})
.then(response => response.json())
.then(data => {
alert(data.result || data.error);
})
.catch(error => {
console.error('Error:', error);
alert('An error occurred while submitting the code.');
});
}