(function(window, document, undefined) { 
    window.onload = init;
    function init() {
        
        //create an empty map
        var map = new Array(960);
        for(var i = 0; i < 960; i++) {
            map[i] = new Array(540);
        }
        
        //get the canvas
        var canvas = document.getElementById("mapcanvas");
        var c = canvas.getContext("2d");
        
        function ncount(i,j) {
            var i1 = i+1;
            var i2 = i-1;
            var j1 = j+1;
            var j2 = j-1;
            var neighbors = 0;
            if(map[i1][j1] == 1){
                neighbors++;
            }
            if(map[i][j1] == 1){
                neighbors++;
            }
            if(map[i2][j1] == 1){
                neighbors++;
            }
            if(map[i1][j] == 1){
                neighbors++;
            }
            if(map[i2][j] == 1){
                neighbors++;
            }
            if(map[i1][j2] == 1){
                neighbors++;
            }
            if(map[i][j2] == 1){
                neighbors++;
            }
            if(map[i2][j2] == 1){
                neighbors++;
            }
            return neighbors;
        }
        
        //when we click, assign values and then paint
        document.getElementById("startbtn").onclick = paint;
        var it = 1;
        function paint() {
            if(it == 1){
                //fill the values
                for(var i = 0; i< 960; i++) {
                    for(var j = 0; j < 540; j++) {
                        if(Math.random() < .45){
                            map[i][j] = 1;
                        } else {
                            map[i][j] = 0;
                        }
                    }
                }
            } else {
                for(var i = 0; i< 960; i++) {
                    for(var j = 0; j < 540; j++){
                        var nc = ncount(i,j);
                        if(nc >= 4 && map[i][j] == 1){
                            map[i][j] == 1;
                        }
                        if(nc >= 5) {
                            map[i][j] == 1;
                        }
                    }
                }
            }
            
            //paint the map
            for(var i = 0; i < 960; i++) {
                for(var j = 0; j< 540; j++){
                    if(map[i][j] == 1){
                        c.fillStyle = '#87CEEB';
                        c.fillRect(i,j,1,1);
                    }
                }
            }
            it++;
            document.getElementById("iter").innerHTML = it;
        }
    }
})(window, document, undefined);

@@ -547,24 +547,86 @@
 ghbors = 0;%0A
+            if(i1 %3C 960 && i2 %3E=0 && j1 %3C 540 && j2 %3E=0)%7B%0A    
             
@@ -655,32 +655,36 @@
                 
+ 
+   
 neighbors++;%0A   
@@ -684,34 +684,42 @@
 ++;%0A            
-%7D%0A
+    %7D%0A    
             if(m
@@ -743,32 +743,36 @@
                 
+    
 neighbors++;%0A   
@@ -772,34 +772,40 @@
 ++;%0A            
-%7D%0A
+    %7D%0A  
             if(m
@@ -792,32 +792,34 @@
 %7D%0A              
+  
 if(map%5Bi2%5D%5Bj1%5D =
@@ -816,32 +816,36 @@
 %5Bi2%5D%5Bj1%5D == 1)%7B%0A
+    
                 
@@ -861,32 +861,36 @@
 ++;%0A            
+    
 %7D%0A            if
@@ -879,32 +879,36 @@
   %7D%0A            
+ 
+   
 if(map%5Bi1%5D%5Bj%5D ==
@@ -920,32 +920,36 @@
                 
+    
 neighbors++;%0A   
@@ -949,34 +949,42 @@
 ++;%0A            
+   
+ 
 %7D%0A
+    
             if(m
@@ -1008,32 +1008,36 @@
                 
+    
 neighbors++;%0A   
@@ -1037,34 +1037,42 @@
 ++;%0A            
+  
-%7D%0A
+  %7D%0A    
             if(m
@@ -1081,32 +1081,36 @@
 %5Bi1%5D%5Bj2%5D == 1)%7B%0A
+    
                 
@@ -1126,34 +1126,42 @@
 ++;%0A            
-%7D%0A
+    %7D%0A    
             if(m
@@ -1185,32 +1185,36 @@
                 
+   
+ 
 neighbors++;%0A   
@@ -1202,32 +1202,36 @@
    neighbors++;%0A
+    
             %7D%0A  
@@ -1232,32 +1232,36 @@
   %7D%0A            
+    
 if(map%5Bi2%5D%5Bj2%5D =
@@ -1274,32 +1274,36 @@
                 
+ 
+   
 neighbors++;%0A   
@@ -1291,32 +1291,50 @@
    neighbors++;%0A
+                %7D%0A
             %7D%0A  

Update script.js