프로그래머스(JavaScript)/Lv1
[프로그래머스 : 연습문제] Lv1. 추억 점수 (JavaScript)
지미지민
2024. 3. 31. 00:04
💜 코드
function solution(name, yearning, photo) {
let result = [];
for(let i=0; i<photo.length; i++){
let totalScore = 0; // 각 그리움 점수
for(let j=0; j<photo[i].length; j++){
let person = photo[i][j];
let idx = name.indexOf(person);
if(idx !== -1) totalScore += yearning[idx];
}
result.push(totalScore);
}
return result;
}
💜 실행 결과
💜 다른 사람의 풀이
function solution(name, yearning, photo) {
let obj = {};
for(let i = 0; i < name.length; i++){
obj[name[i]] = yearning[i];
}
return photo.map(value => value.map(v => obj[v] ? obj[v] : 0).reduce((acc,cur) => acc + cur,0))
}