❓문제 설명
- 한 변의 길이가 n인 정사각형이 있음 변의 길이만큼 블록이 나올 것임 ex)4 x 4
- 각 블록을 겹처서 최종적으로 하나의 블록을 만들어야 함
- 블록이 한군데라도 칠해져있다면 칠한채로 둘다 칠해지지 않았다면 안칠한채로 합칠 것임
- 주어지는 값은 변의 길이인 n, 배열 2개 (2진법으로 바꿔야하는 것임)
- 출력값으로 비밀지로를 만들어야 함 ex) ["#####","# # #", "### #", "# ##", "#####"]
✅ 문제 해결
- 배열 두개를 index값을 통해 가져와서 합칠 것임 그리고 그 합친 값에 대해서 0 = " ", 1 = "#"으로 바꿀 것임
- 이진법으로 나타내는 방법을 알아야 함 문자열.toString(2)
- 원하는 길이만큼 만들어야하는데 둘 다 앞이 0일때는 길이가 점점 줄어든다 그래서 채워야함 문자열.padStart(n, " ")
- 문자열을 나눴다가 합치는 과정 split(""), join("")
function solution(n, arr1, arr2) {
var answer = [];
for (let i = 0; i < n; i++) {
const sum = Number(arr1[i].toString(2)) + Number(arr2[i].toString(2))
answer.push(sum.toString().split("").map(a => a > 0 ? "#" :" " ).join("").padStart(n, " "))
}
return answer;
}
❕느낀점
- split() 과 join()이 바로 생각이 안난 것을 보니 확실히 알고리즘을 다시 시작해야겠다.