var orangesRotting = function(grid) {
const numRows = grid.length;
const numCols = grid[0].length;
for (let row = 0; row < numRows; row++) {
for (let col = 0; col < numCols; col++) {
if (grid[row][col] === 2) {
} else if (grid[row][col] === 1) {
if (numFreshOranges === 0) {
while (queue.length > 0) {
let numRottenOranges = queue.length;
while (i < numRottenOranges) {
const [currentRow, currentCol] = queue.shift();
const directions = [[0,1],[0,-1],[1,0],[-1,0]];
directions.forEach((direction) => {
const [rowOffset, colOffset] = direction;
const newRow = currentRow + rowOffset;
const newCol = currentCol + colOffset;
if (newRow >= 0 && newRow < numRows && newCol >= 0 && newCol < numCols) {
if (grid[newRow][newCol] === 1) {
grid[newRow][newCol] = 2;
queue.push([newRow, newCol]);
return numFreshOranges === 0 ? minutesElapsed : -1;