Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Moore Neighbourhood - Second Solution solution in Clear category for [old] Moore Neighbourhood by cyw617
"use strict";
function countNeighbours(data, row, col) {
let count = 0;
for (let i = row - 1; i <= row + 1; i++) {
for (let j = col - 1; j <= col + 1; j++) {
count += (data[i] && data[i][j]) ? data[i][j] : 0;
}
}
return count - data[row][col];
}
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!");
}
Sept. 18, 2016
Comments: