Array.prototype.map() - JavaScript | MDN
<aside> π‘ reduce()μ μ΄κΈ°κ°μ 0μ΄λ μ«μμ΄ μ΄μΈμ λ¬Έμμ΄, λ°°μ΄, κ°μ²΄λ κ°λ₯νλ€! β μ΄κΈ°κ°μ λ°λΌμ reduce()κ° μ΅μ’ μ μΌλ‘ λ°μμ€λ νμ μ΄ κ²°μ λ μλ μμ!
</aside>
Array.prototype.reduce() - JavaScript | MDN
// <https://programmers.co.kr/learn/courses/30/lessons/17681>
function solution(n, arr1, arr2) {
const answer = []
for( let i = 0 ; i < arr1.length ; i++){
answer[i] = ""
// μ§λ1μ 2μ§λ²μΌλ‘ λ³νν λ°μ΄ν°λ₯Ό μ μ₯
const map1 = arr1[i].toString( 2 ).padStart(n, "0")
// μ§λ2λ₯Ό 2μ§λ²μΌλ‘ λ³νν λ°μ΄ν°λ₯Ό μ μ₯
const map2 = arr2[i].toString( 2 ).padStart(n, "0")
for (let j = 0; j<map1.length; j++){
// λ μ€ νλλΌλ λ²½μ΄κΈ° λλ¬Έμ μ μ μ§λμμλ λ²½
if (map1[j] ==="1" || map2[j] === "1"){
answer[i] +="#"
}
// λ κ°μ μ§λ λͺ¨λ 곡백μ΄λΌλ©΄, μ 체 μ§λμμλ 곡백
else if (map1[j] === "0" && map2[j] ==="0"){
answer[i] += " "
}
}
}
return answer
}
// map()κ³Ό reduce()λ₯Ό μ¬μ©ν νμ΄
function solution(n, arr1, arr2) {
const answer = arr1.map( (map1,i) => {
map1 = map1.toString( 2 ).padStart(n, "0")
const map2 = arr2[i].toString( 2 ).padStart(n,"0")
const result = map1.split("").reduce( (acc,cur,i) => {
return acc += ( cur === "1" || map2[i] === "1")
? "#"
: " "
}, "")
return result
})
return answer
}
<aside>
β ν
μ€νΈ λ°μ΄ν° μ΄μΈμ λ°μ΄ν°(νΉν μ΄λ―Έμ§ λ°μ΄ν°
) μ²λ¦¬λ μ΄λ»κ² ν κ²μΈκ°?
μ΄λ―Έμ§λ₯Ό λ€λ₯Έ λͺ¨λ°μΌ νκ²½μμλ μ΄λ»κ² 보μ΄κ² ν μ§, μ¬μ΄μ¦λ νμ§μ μ΄λ»κ² μ²λ¦¬ν κ²μΈμ§(μ±λ₯ λ±)
</aside>