Single pass JavaScript solution
We can execute all the logic in one pass through the box matrix by making those observations:\n given the box[i][j] element in the source array, its equivalent 
We can execute all the logic in one pass through the box matrix by making those observations:\n* given the `box[i][j]` element in the source array, its equivalent index in the target (answer) array will be `ans[j][R - i - 1]` (where R is the total number of rows in the source matrix);\n* we can iterate through the source columns from the end to begining to "simulate gravity" ;\n* we can maintain the stone insertion index in the target matrix (and update this index whenever we hit an obstacle).\n\nHere is the complete code:\n\n```javascript\nvar rotateTheBox = function(box) {\n    const R = box.length;\n    const C = box[0].length;\n    \n    const ans = Array.from({length: C}, () => Array.from({length: R}, () => \'.\'));\n    \n    for (let i = 0; i < R; i++) {\n        \n        // maintain the index of stone insertion\n        let insRow = C - 1;\n        \n        for (let j = C - 1; j >=0; j--) {\n            const el = box[i][j];\n            if (el === \'*\') {\n                ans[j][R - i - 1] = \'*\';\n                insRow = j - 1;\n            } else if (el === \'#\') {\n                ans[insRow][R - i - 1] = \'#\';\n                insRow--;\n            }\n        }\n    }\n    \n    return ans;\n};\n```
['Matrix', 'JavaScript']