문제
정답
const fs = require("fs");
const filePath = process.platform === "linux" ? "/dev/stdin" : "test.txt";
const input = fs
.readFileSync(filePath)
.toString()
.split("\n")
.map(_ => _.split(""));
// 배열에서 가장 긴 문자열의 길이 구하기
const maxStrNum = Math.max.apply(
null,
input.map(str => str.length)
);
let answer = [];
for (let j = 0; j < maxStrNum; j++) {
for (let i = 0; i < input.length; i++) {
// 배열의 해당 위치에 요소가 없는 경우 예외 처리
input[i] && input[i][j] && answer.push(input[i][j]);
}
}
console.log(answer.join(""));
반응형