previous

page 21

next

It worked ok but I don't like to see a great swag of code repeated so I rewrote it as follows:

a more succinct counter_clicked function
function counter_clicked(el) {
var id=el.id;
var n=id.substring(1);
if (n<10) {
var row=n;
for (var col=0;col<10;col++) fill(row,col);
} else {
var col=n-10;
for (var row=0;row<10;row++) fill(row,col);
}
}
function fill(row,col) {
var id=rc2id(row,col);
var el=document.getElementById(id);
var v=el.getAttribute('v');
if (v==0) {
var a=get_a(row,col);
if (a==7) {
el.src='images/right/7.png';
} else {
el.src='images/wrong/7.png';
}
}
}

You'll have noticed the new JavaScript word, substring.
As usual, we start counting at 0 so the 1 means to start at the second character.

previous
next