-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathjavascript-excecutor.js
More file actions
94 lines (81 loc) · 2.79 KB
/
javascript-excecutor.js
File metadata and controls
94 lines (81 loc) · 2.79 KB
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
document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById('challengeForm');
form.addEventListener('submit', function (event) {
event.preventDefault(); // Stop form from submitting
let isValid = true;
clearErrors();
// Validate title
const title = document.getElementById('title');
if (title.value.trim() === '') {
showError(title, 'Title is required');
isValid = false;
}
// Validate description
const description = document.getElementById('description');
if (description.value.trim() === '') {
showError(description, 'Description is required');
isValid = false;
}
// Validate difficulty
const difficulty = document.getElementById('difficulty');
if (difficulty.value === '') {
showError(difficulty, 'Please select a difficulty level');
isValid = false;
}
// Validate test cases
const testCases = document.querySelectorAll('.test-case');
testCases.forEach((tc, index) => {
if (tc.value.trim() === '') {
showError(tc, `Test case ${index + 1} cannot be empty`);
isValid = false;
}
});
// Validate solution
const solution = document.getElementById('solution');
if (solution.value.trim() === '') {
showError(solution, 'Sample solution is required');
isValid = false;
}
if (isValid) {
alert('Challenge submitted successfully!');
form.reset(); // Clear the form
}
});
function showError(inputElement, message) {
let error = document.createElement('div');
error.className = 'error-message';
error.innerText = message;
inputElement.parentNode.appendChild(error);
inputElement.style.borderColor = 'red';
}
function clearErrors() {
document.querySelectorAll('.error-message').forEach((e) => e.remove());
document.querySelectorAll('input, textarea, select').forEach((el) => {
el.style.borderColor = '#ccc';
});
}
});
document.getElementById('downloadCode').addEventListener('click', () => {
const code = document.querySelector('#codeEditor').value; // Adjust selector if needed
const blob = new Blob([code], { type: 'text/plain' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
// Optional: name the file based on selected language
const language = document.querySelector('#languageSelect').value || 'code';
const extension = getFileExtension(language);
link.download = `my_code.${extension}`;
link.click();
});
function getFileExtension(language) {
// Map language names to common extensions
const extensions = {
'javascript': 'js',
'python': 'py',
'java': 'java',
'c': 'c',
'cpp': 'cpp'
// Add more languages if needed
};
const key = language.toLowerCase();
return extensions[key] || 'txt';
}