I now have the code that generates the grids. This task will be completed for 10 minutes, e.g., and participants will be asked to answer as many questions as possible. each time they enter an answer for the number of shaded squares in the grid, a new grid appears. My current problem is that after an answer has been given, the program needs to assess whether the answer is correct or not, and load a new grid. This process should repeat itself until the end of the 10 minutes and the number of correct answers needs to be updated after the program verifies whether the answer is correct or not. The task is programmed with the PyCharm editor and I currently am not able to have this sequences repeating itself until the end of the time set for the task.
Code:
{% block styles %}
<style>
table, tr, td {
border: 1px solid #000000;
}
.bg-black {
background-color: #808080;}
</style>
{% endblock styles %}
{% block scripts %}
<script>
var effort_correct, effort_incorrect, effort_attempt, answer;
var matrix = [];
var height = Math.floor((Math.random() * 7) + 5);
var width = Math.floor((Math.random() * 7) + 5);
var gridSize = height * width;
var solution = 0;
for (var i = 0; i < height; i++) {
matrix[i] = [];
for (var j = 0; j < width; j++) {
if (Math.random() < 0.5) {
matrix[i][j] = 1;
solution++;
} else {
matrix[i][j] = 0;
}
}
}
function render(grid) {
var html = '<table><tbody>';
for (var k = 0; k < height; k++) {
html += '<tr>';
for (var l = 0; l < width; l++) {
html += grid[k][l] ? '<td class="bg-black"> </td>' :
'<td> </td>';
}
html += '</tr>'
}
html += '</tbody></table>';
return html;
}
document.querySelector('#grid').innerHTML = render(matrix);
function score(answer) {
if (answer == solution) {
effort_correct++;
} else {
effort_incorrect++;
}
effort_attempt++;
}
</script>
{% endblock scripts %}
{% block title %}
Task 1: Stage 1
{% endblock %}
{% block content %}
<head>
<style>
p.text-center {text-align: center;}
p.thick {font-weight: bold;}
</style>
</head>
<p-class text-center>
<div id="grid"></div>
<br>
<br>
{% formfield player.answer with label="The number of shaded squares is:" %}
<br>
<br>
Number of correct answers: {{score}}
</p-class>
<br>
<br>
<button onclick="score(player.answer)">Submit</button>
{% endblock %}