previous

page 17

next
no return before the end

It is considered a bad practice to use return anywhere but at the end of a function. Here's how to rewrite computer() to avoid this.

function computer() {
var done=false;
var ids=[];
for (var i=0;i<9;i++) ids[i]=i;
Shuffle(ids);
for (var i=0;i<9;i++) {
var id=ids[i];
if (g_squares[id]==0) { // try this square
g_squares[id]=1;
if (check()) { // no good -it's a loser!
var id_loser=id; // save it in case we have to use it
g_squares[id]=0; // undo move
} else {
do_x(id);
g_player=true;
done=true; break; // all done
}
}
}
if (!done) {
// no good move found - make id_loser move & lose
do_x(id_loser);
check();
show_result();
g_message_el.textContent='you win';
g_player_count++;
var el=document.getElementById('player');
el.textContent=g_player_count;
g_another_el.style.visibility='visible';
}
}
previous
next