如何创建一个方法来验证数组的范围?

时间:2015-04-07 13:25:42

标签: javascript arrays angularjs object methods

我有一个具有某些属性的对象。我想创建一个方法,如果数组的位置在范围内,则返回,但我不知道如何创建它。 (我试图在AngularJS中制作扫雷,所以这段代码在.factory内)

我的代码如下:

var tableSquare = function(num_rows, num_bombs){
        this.numRows = num_rows;
        this.numBombs = num_bombs;
        this.positions = new Array(numRows);
        for (i = 0; i < numRows; i++) {
            this.positions[i] = new Array(numRows);
        }
};

现在我有一个工作函数inRange(值必须是&gt; = 0和&lt;数组的范围):

var inRange = function(i,j, range){
    if (i>=0 && i<= range-1 && j>=0 && j<=range-1)
        return true;
    return false;
};

所以我可以看看我的数组中的一个元素是否在范围内,我使用它(numBombs它是我在每个tableSquare.positions中插入的对象的属性):

if (inRange(i-1,j-1,this.numRows)) this.positions[i-1][j-1].numBombs += 1; //up left

我想要一个类似的方法:

tableSquare.prototype.inRange = function(){
    //I don't know how to read here i and j
    //in this.positions[i][j]
};

所以我可以使用这个函数制作:

if (this.positions[i-1][j-1].inRange()) this.positions[i-1][j-1].numBombs += 1;

如何创建此inRange方法?谢谢;)

2 个答案:

答案 0 :(得分:0)

你的tableSquare应该是

 var tableSquare = function(num_rows, num_bombs){
    this.numRows = num_rows;
    this.numBombs = num_bombs;
    this.positions = new Array(this.numRows);

    for (var i = 0; i < this.numRows; i++) {
        this.positions[i] = new Array(this.numRows);
    }
    this.inRange = function(i,j, range){
      if (i>=0 && i<= range-1 && j>=0 && j<=range-1)
         return true;
      return false;
    };
};

然后你应该创建一个tableSquare的对象来做你的操作。

 var objectOne= new tableSquare(4,4)

您可以按如下方式调用inRange()

objectOne.inRange(1,2,3)

在这里学习javascript:

1)http://eloquentjavascript.net/

2。)https://stackoverflow.com/tags/javascript/info

答案 1 :(得分:0)

感谢所有评论。我想要的是这样的:

object.positions[1][4].inRange()

这个inRange函数应该读取1和4的位置和位置[0]的长度,并验证它们是否在de array中。我不知道如何读取这个索引(实际上我是这样做的,但是将这些坐标添加到positios中的objetc,重复信息)。

所以我使用Adeian May的解决方案“getNeighbours()”如下:

var tableSquare = function(num_rows, num_bombs){
    this.numRows = num_rows;
    this.numBombs = num_bombs;

    this.positions = new Array(num_rows);
    for (var i = 0; i < num_rows; i++)
        this.positions[i] = new Array(num_rows);

    this.findingNeighbors = function (i, j) {
        var rowLimit = this.positions.length-1;
        var columnLimit = this.positions[0].length-1;
        var neighbors = [];

        for(var x = Math.max(0, i-1); x <= Math.min(i+1, rowLimit); x++)
            for(var y = Math.max(0, j-1); y <= Math.min(j+1, columnLimit); y++)
                if(x !== i || y !== j)
                    neighbors.push([x,y]);

        return neighbors;
    };
};

并将其调用如下,因此我只与邻居合作,而不必验证他们是否在范围内:

 neighbors = this.findingNeighbors(i,j).slice();
 for (x = 0; x < neighbors.length; x++)
    this.positions[neighbors[x][0]][neighbors[x][1]].numBombs +=1;

多数工作,但仍然不知道在没有传递参数的情况下读取数组中的索引是否可行,例如:

(seudocode)
table.positions.inRange = function(){
    var position x = read first index of the array (positions[x])
    var position y = read second index of the array (positions[y])
    lenght-x = positions.length
    and so on...

然后使用xxxxx.positions [1] [4] .inRange()我可以读取这个“1”索引和“4”索引。     }