240512
문제 : 추억 점수 난이도 : Lv.1 설명 :
문자열로 구성된 리스트 strings와, 정수 n이 주어졌을 때, 각 문자열의 인덱스 n번째 글자를 기준으로 오름차순 정렬하려 합니다. 예를 들어 strings가 ["sun", "bed", "car"]이고 n이 1이면 각 단어의 인덱스 1의 문자 "u", "e", "a"로 strings를 정렬합니다.
풀이
function solution(name, yearning, photo) {
var answer = [];
for(let i = 0; i < photo.length; i++) {
let sum = 0;
for(let j= 0; j < photo[i].length; j++) {
let index = name.indexOf(photo[i][j])
if(index !== -1) {
sum += yearning[index]
} else {
sum += 0
}
console.log(sum)
if(j === photo[i].length -1) {
answer.push(sum)
}
}
}
return answer;
}
👀 다른 사람 풀이
function solution(name, yearning, photo) {
return photo.map((v)=> v.reduce((a, c)=> a += yearning[name.indexOf(c)] ?? 0, 0))
}
Last updated