I have a "class" called Block
. It possesses three variables : x
, y
, and z
, which are its three-dimensional coordinates, respectively. I am having trouble finding the number of "neighbours" to a block. That is, if this
shares a face, edge, or vertex with other
, it should return true. So, block.neighbours()
returns the number of neighbours to block
. However, it is returning 0
rather than the expected 1
. My code is shown below:
var blocks = [];
function Block(x, y, z) {
blocks.push(this);
this.x = x;
this.y = y;
this.z = z;
this.neighbours = function() {
var n = 0;
for (var block in blocks) {
if (block == this) {
continue;
}
if (Math.abs(block.x - this.x) <= 1 && Math.abs(block.y - this.y) <= 1 && Math.abs(block.z - this.z) <= 1) {
n++;
}
}
return n;
};
}
var b1 = new Block(0, 0, 0);
var b2 = new Block(0, 1, 0);
document.getElementById("box").innerHTML = b1.neighbours();
Why is the function returning 0
? (Note: Before the javascript I have the html <p id = "box"></p>
, and in the paragraph it shows 0
).