Aaron recommended breaking, which is one option.
Another option is to use setTimeout
to generate a map, then if that fails, generate another.
Using the setTimeout
you can make sure it doesn't block the browser.
function generateMap() {
map = [];
for (i1 = 0; i1 < size; i1++) {
s = '';
for (i2 = 0; i2 < size; i2++) {
var r = getRandomInt(1,oneIn);
if (r == oneIn) s += '1';
else s += '0';
}
map.push(s);
}
if (!mapValid(map)) {
console.log("Generating another map.");
// Note: There is no () after generateMap. We are passing the function reference.
window.setTimeout(generateMap, 25);
} else {
writeToPage(map, 'map');
}
}