function solution(name, yearning, photo) {
var answer = [];
let score = {};
for (let i = 0; i < name.length; i++){
score[name[i]] = yearning[i];
}
photo.map(a => {
let result = 0;
for(let j of a){
score[j] !== undefined ? result += score[j] : result += 0;
}
answer.push(result);
})
return answer;
}
function solution(name, yearning, photo) {
return photo.map((v)=> v.reduce((a, c)=> a += yearning[name.indexOf(c)] ?? 0, 0))
}
나는 새로 스코어를 만들고 거기에 배정을 한 다음 이름이 있을 경우에 점수를 더하고 없을 경우에는 0을 더해서 answer 배열에 추가했다.
다른 답을 보면 yearning
에서 name.indexOf(c)
를 통해 해당 숫자를 가져오고 그 숫자가 없다면 undefined
가 반환된다. 하지만 ??
연산자를 통해 null
또는 undefined
일 경우 0을 반환한다.
이렇게 원하는 결과값을 배열로 받을 수 있다.