Find minimum distance bugged

The function minDist() takes an array of coordinates from an object, iterates over those coordinates, and returns the smallest distance from the coordinates to a filled cell(a value of 1) in a 10x20 array. The return value is then added to the object’s y value. It seems to work correctly, but when I perform the function .rotate(), which alters the object’s coordinates, and then perform the function minDist(), the loops within minDist() seem to iterate much longer than they’re supposed to, resulting in the y-value of an object getting really weird. I’m not sure if there’s a problem with .rotate() or minDist(). Both the functions and the object they’re being performed on are being exported to another file called keys.js. Not sure if there’s some sort of error in importing/updating the values of certain variables being passed between the two files. It seems like the object’s coordinates are having trouble being updated.

https://replit.com/@arn5891/tetris#script.js

//rotate function
rotate(dir){
    let store = [];
    //dir=0:cw dir=else:ccw
    if(dir===0){
      for(let a = 0; a < this.piece.length; a++){
      store.push([this.piece[a][1]*-1,this.piece[a][0]])
      }
    }
      
    else{
      for(let a = 0; a < this.piece.length; a++){
        store.push([this.piece[a][1],this.piece[a][0]*-1])
      }
    }
    this.piece = store;
  }
//function minDist
export let minDist = function(obj){
  let p = obj.piece
  let min = Infinity;
  let track = 0;
  for(let b = 0; b < p.length; b++){
    for(let a = obj.y+p[b][1]; a < grid.length; a++){
      if(grid[a][obj.x+p[b][0]]==1){
        let dist = (a-obj.y+p[b][1]);
        console.log(dist);
        if(dist < min){
          min = dist
        }
     }
    }
  }```