Skip to content

Commit

Permalink
Updated assignments.html to fix short answers!
Browse files Browse the repository at this point in the history
  • Loading branch information
donutStudio authored Jul 13, 2024
1 parent fc741a3 commit 2d55583
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions assignments.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ <h2>Upload Assignment File</h2>

<script>
let correctAnswers = {}; // Store correct answers here
let mathFieldMap = new Map(); // Map to store MathQuill instances

document.getElementById('file').onchange = function() {
var file = this.files[0];
Expand Down Expand Up @@ -216,8 +217,6 @@ <h2>Upload Assignment File</h2>
}

function generateAnswerHTML(answerField, variables, uniqueQuestionId) {
if (!answerField) return '';

var typeIndex = answerField.indexOf('type:');
var typeValue = answerField.substring(typeIndex + 5).trim();

Expand All @@ -229,10 +228,9 @@ <h2>Upload Assignment File</h2>
var optionsString = typeValue.substring(optionsStartIndex, optionsEndIndex);
var options = optionsString.split(',').map(option => evaluateAndSimplifyExpression(option.trim(), variables));

// Check if randomization is requested
var randomizeIndex = typeValue.indexOf('randomize=true');
if (randomizeIndex !== -1) {
options = shuffleArray(options); // Assuming shuffleArray function is defined
options = shuffleArray(options);
}

var choicesHTML = options.map(option => '<label><input type="radio" name="multiple-choice-' + uniqueQuestionId + '"> ' + option + '</label>').join('<br>');
Expand All @@ -241,7 +239,6 @@ <h2>Upload Assignment File</h2>
return '';
}

// Function to shuffle array elements (Fisher-Yates shuffle)
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
Expand Down Expand Up @@ -270,15 +267,14 @@ <h2>Upload Assignment File</h2>
spaceBehavesLikeTab: true,
handlers: {
edit: function() {
var enteredMath = field.latex();
var enteredMath = mathField.latex();
console.log('Current LaTeX: ' + enteredMath);
}
}
});
field.dataset.mathquillField = mathField;
mathFieldMap.set(field.id, mathField); // Store MathQuill instance in Map
});

// Add click event listeners to check buttons for multiple-choice questions
document.querySelectorAll('button[id^="check-button-"]').forEach(function(button) {
button.addEventListener('click', function() {
var uniqueQuestionId = this.id.split('-').slice(2).join('-');
Expand All @@ -291,10 +287,7 @@ <h2>Upload Assignment File</h2>
var answerType = document.querySelector('input[name="multiple-choice-' + uniqueQuestionId + '"]') ? 'multiple-choice' : 'short-answer';

if (answerType === 'multiple-choice') {
// Get the correct answer for this question
var correctAnswer = correctAnswers[uniqueQuestionId];

// Get the selected answer
var selectedAnswer = document.querySelector('input[name="multiple-choice-' + uniqueQuestionId + '"]:checked');

if (selectedAnswer) {
Expand All @@ -308,22 +301,32 @@ <h2>Upload Assignment File</h2>
alert('Please select an answer.');
}
} else if (answerType === 'short-answer') {
// Get the correct answer for this question
var correctAnswer = correctAnswers[uniqueQuestionId];
var mathField = mathFieldMap.get('answer-' + uniqueQuestionId); // Retrieve MathQuill instance from Map

if (mathField) {
console.log('mathField:', mathField); // Debugging line
console.log('typeof mathField:', typeof mathField); // Debugging line

var enteredAnswer;
try {
enteredAnswer = mathField.latex();
} catch (error) {
console.error('Error accessing latex method:', error);
alert('Error accessing the answer field.');
return;
}

// Get the MathQuill field
var mathField = document.querySelector('#answer-' + uniqueQuestionId).dataset.mathquillField;

// Get the entered answer
var enteredAnswer = mathField.latex();

if (enteredAnswer === correctAnswer.toString()) {
alert('Correct!');
if (enteredAnswer === correctAnswer.toString()) {
alert('Correct!');
} else {
alert('Incorrect. Please enter the correct answer.');
}
} else {
alert('Incorrect. Please enter the correct answer.');
alert('MathQuill field not found.');
}
}
}
</script>
</body>
</html>
</html>

0 comments on commit 2d55583

Please sign in to comment.