By comparing two arrays with same numbers, IndexOf returns false.
It also doesn´t work for single numbers e.g. position[i].location[j]. I would like to compare the whole array, not only single ints. [32, 33, 34] & [35, 34, 33] should also return true... Is there a better way?
(...)
//Array of ship locations
ships: [
{location:[13, 23, 33]},
{location:[2, 12, 22]},
{location:[15, 14, 16]},
{location:[17, 18, 19]},
{location:[31, 41, 51]},
{location:[40, 41, 42]}
],
//Array with additional ship location
position: [
{location: [2, 12, 22]},
{location: [40, 41, 42]}
],
collision: function(position) {
if(this.ships.length > 1) {
for(var i = 0; i < this.ships.length; i++) {
for(var j = 0; j < position.length; j++) {
if(this.ships[i].location.indexOf(this.position[j].location) >= 0) {
return true;
} else {
return false;
}
}
}
} else {
return false;
}
}
console.log(model.collision(model.position)); //returns false