previous

page 8

next

Check this all works ok and keep going until you notice that we have an issue to deal with apart from the score display

What is the issue? click me for the answer

How can we fix it? click me for the answer

function get_pic() {
clear();
Shuffle(g_ind);
g_answer_id=Random0(4);
var pic_n=g_ind[g_answer_id];
if (g_used.includes(pic_n)) {
get_pic();
} else {
g_used.push(pic_n);
var el=document.getElementById('pic');
el.src='images/'+pic_n+'.jpg';
for (var i=0;i<5;i++) {
var el=document.getElementById(i);
var pic_n=g_ind[i];
el.textContent=g_pics[pic_n];
}
}
}

Note the new global, g_used which must be initialised to [] and the new JavaScript function, includes.

Also note that recursively calling get_pic() is not a good idea. In fact this program will hang if you try to make a quiz with more questions than entries.

One way to avoid this pitfall is to limit the number of times get_pic calls itself by means of a global counter, g_k. This of course raises the problem of what to do when the limit is exceeded. I'll show you my full solution on the next page but don't forget to add this to your globals.js:

var g_k=0;
previous
next