문제 출처 : https://programmers.co.kr/learn/courses/30/lessons/17681?language=javascript
const transBinary = (n,max) => {
const binary = n.toString(2);
return '0'.repeat(max - binary.length) + binary
}
const decoding = (map1, map2) => {
return map1.split('').reduce((prev,curr,i)=>{
return prev + ((curr === '1'|| map2[i] === '1') ? '#' : ' ')
},'');
}
function solution(n, arr1, arr2) {
return arr1.map((num,i)=>{
const map1 = transBinary(num, n);
const map2 = transBinary(arr2[i], n);
return decoding(map1, map2);;
});
}
function solution(n, arr1, arr2) {
return arr1.map((num,i)=>(num|arr2[i])
.toString(2).padStart(n, '0')
.replace(/1/g,'#')
.replace(/0/g,' ')
);
}
현재 문자열의 시작을 다른 문자열로 채워, 주어진 길이를 만족하는 새로운 문자열을 반환한다.
// 사용 전
const binary = n.toString(2);
return '0'.repeat(max - binary.length) + binary
// 사용 후
return n.toString(2).padStart(max, '0')
비트 연산자 정리: https://blankspace-dev.tistory.com/425
const num1 = 9
const num2 = 30
const max = 5;
// 사용 전
const map1 = num1.toString(2).padStart(max, '0');
const map2 = num2.toString(2).padStart(max, '0');
const result = map1.split('').reduce((prev,curr,i) => {
return prev + ((curr === '1'|| map2[i] === '1') ? '1' : '0')
},'');
console.log(result); // 11111
// 사용 후
const result = (num1|num2).toString(2).padStart(max, '0') // 👍👍👍👍
console.log(result); // 11111