문제 🔽
https://school.programmers.co.kr/learn/courses/30/lessons/12949
다른사람 풀이 🔽
function solution(arr1, arr2) {
return arr1.map((row) => arr2[0].map((x,y) => row.reduce((a,b,c) => a + b * arr2[c][y], 0)))
}
내풀이🔽
1. 실패 ❌ => ( answer[i][j] => 이러면 안됨)
function solution(arr1, arr2) {
var answer = [[]];
for(let i=0; i<arr1.length; i++){
for(let j=0; j<arr2[0].length; j++){
let sum = 0;
for(let z=0; z<arr1[0].length; ++z){
sum+=arr1[i][z]*arr2[z][j]
}
answer[i][j]=sum
}
}
return answer;
}
2. 성공 ⭕
function solution(arr1, arr2) {
let answer =[];
for(let i=0; i<arr1.length; i++){
let newArr=[]
for(let j=0; j<arr2[0].length; j++){
let sum = 0;
for(let z=0; z<arr1[0].length; ++z){
sum+=arr1[i][z]*arr2[z][j]
}
newArr.push(sum)
}
answer.push(newArr)
}
return answer;
}
느낀점🔽
첫 시도에 로직을 파악 할 수 있었다.
객체에 해당 원소를 넣는 answer[i][j]=sum 에서 틀렸다 ◀array 원소 값 할당은 좌측과 같이 안된다는 사실을 알게 됐다.
그래서 push를 이용하여 array에 값을 할당했다.
'Javascript' 카테고리의 다른 글
프로그래머스 코딩테스트 풀이(js) > 튜플(lv2) (0) | 2023.07.05 |
---|---|
프로그래머스 코딩테스트 풀이(js) > 의상(lv2) (0) | 2023.07.04 |
프로그래머스 코딩테스트 풀이(js) > n^2 배열 자르기(lv2) (0) | 2023.07.01 |
프로그래머스 코딩테스트 풀이(js) > 연속 부분 수열 합의 개수(lv2) (0) | 2023.06.30 |
프로그래머스 코딩테스트 풀이(js) > H-index(lv2) (0) | 2023.06.29 |