JSFiddle - React, Tailwind, and code Playground
by kalar su
HTML
<div id="arr">
Find rectangler from following array:<br>
</div>
<br>Rec count as:
<div id="result"></div>
JavaScript
var image = [
[1, 1, 1, 0, 0, 1, 1],
[0, 1, 0, 1, 0, 1, 1],
[1, 1, 1, 0, 0, 0, 1],
[1, 1, 1, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1]
];
function countRec(arr){
var pos = new Array ;
var countRec = 0;
image.forEach(function(item, key){
var width = 0;
var x = 0;
item.reduce(function(pre, next, index){
if(pre==0 && next==0){
width ++;
if(width==1) x = index;
}
return next;
},1);
if(width>0) width++;
pos[key] = [x,width];
if(key!=0 && pos[key].toString() == pos[key-1].toString()){
}else if(width>1){
countRec ++;
}
});
return countRec;
}
var numRec = countRec(image);
var arrNode = document.getElementById("arr");
image.forEach(function(item,key){
arrNode.innerHTML += item + "<br>";
});
document.getElementById("result").innerHTML = numRec;