😎풀이

  1. ruleKey를 활용하여 찾고자 하는 인덱스 검색
  2. items 순회
    2-1. 현재 요소의 값이 ruleValue에 해당하는지 비교
    2-2. 해당될 경우 matched 증가
  3. 확인된 matched 반환
function countMatches(items: string[][], ruleKey: string, ruleValue: string): number {
    const findIdx = keyToIdx(ruleKey)
    let matched = 0
    for(let i = 0; i < items.length; i++) {
        const curValue = items[i][findIdx]
        if(curValue === ruleValue) matched++
    }
    return matched
};

function keyToIdx(ruleKey: string) {
    switch(ruleKey) {
        case 'type':
            return 0
        case 'color':
            return 1
        case 'name':
            return 2
    }
    return  0
}
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글