Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second - (Little bit of tweaking) solution in Clear category for [old] Moore Neighbourhood by flairplaya
"use strict";
function countNeighbours(g, r, c) {
/*
var res = 0;
for(var i = r-1; i <= r+1; i++) {
if(!g[i]) continue;
for(var j = c-1; j <= c+1; j++) {
res += g[i][j] && (i !== r || j !== c) ? 1 : 0;
}
}
return res;
*/
var res = 0;
for(var i = r-1; i <= r+1; i++) {
for(var j = c-1; j <= c+1; j++) {
g[i] && g[i][j] ? res++ : res;
}
}
return res - g[r][c];
}
var assert = require('assert');
if (!global.is_checking) {
assert.equal(countNeighbours([[1, 0, 0, 1, 0],
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 1],
[1, 0, 0, 0, 0],
[0, 0, 1, 0, 0]], 1, 2), 3, "1st example");
assert.equal(countNeighbours([[1, 0, 0, 1, 0],
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 1],
[1, 0, 0, 0, 0],
[0, 0, 1, 0, 0]], 0, 0), 1, "2nd example");
assert.equal(countNeighbours([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]], 0, 2), 3, "Dense corner");
assert.equal(countNeighbours([[0, 0, 0],
[0, 1, 0],
[0, 0, 0]], 1, 1), 0, "Single");
console.log("Coding complete? Click 'Check' to review your tests and earn cool rewards!");
}
Jan. 15, 2019