function solution(n) {
let answer = [];
for(let i = 1; i<= n; i++) {
answer.push(Array.from({length: i}, () => 0));
}
let startRow = 0;
let startColumn = 0;
let endRow = n-1;
let endColumn = n-1;
let cnt = 0;
let num = 1;
while(startRow <= endRow && startColumn <= endColumn) {
for(let i = startRow; i <= endRow; i++) {
answer[i][startColumn] = num;
num++
}
startRow++;
startColumn++;
for(let i = startColumn; i <= endColumn; i++) {
answer[endRow][i] = num;
num++
}
endColumn--;
endRow--;
for(let i = endRow; i>= startRow; i--) {
answer[i][answer[i].length -1 -cnt] = num;
num++;
}
startRow++;
endColumn--;
cnt++;
}
return answer.flat()
}
참고
https://velog.io/@proshy/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4javascript-%EC%82%BC%EA%B0%81-%EB%8B%AC%ED%8C%BD%EC%9D%B4