-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12-16.js
More file actions
33 lines (30 loc) · 838 Bytes
/
12-16.js
File metadata and controls
33 lines (30 loc) · 838 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function countIslands (mapStr) {
var counter =0;
var first = mapStr.split("\n")
var array = [];
first.forEach(function(item){
array.push(item.split(""));
})
for(var i=0;i<array.length;i++){
for (var j=0; j <array[i].length; j++){
if (array[i][j] == "0"){
if (i > 0 && array[i-1][j] !== "0" && j > 0 && array[i][j-1] !=="0"){
counter++;
}
else if( i === 0 && j > 0 && array[i][j-1] !== "0"){
counter++;
}
else if( j === 0 && i > 0 && array[i-1][j] !== "0"){
counter++;
}
else if (i === 0 && j === 0){
counter++;
}
else if(i > 0 && array[i-1][j] === "0" && j > 0 && array[i][j-1] === "0" && array[i-1][j-1] !== "0" ){
counter--;
}
}
}
}
return counter;
}