문자 찾기

jinny·2021년 8월 24일

Algorithm

목록 보기
6/34
post-thumbnail

'COMPUTERPROGRAMMING'에서 R 문자가 몇개인지 찾기

  1. for문 이용
function solution(str, t){

    let result = 0;
    for(let x of str) {
        if(x==t) result++;
        console.log(x);
        
    }
    

    return result;
}

let str = 'COMPUTERPROGRAMMING' ;
console.log(solution(str,'R'));
  1. split() 메서드 이용
function solution(str, t){

    let result = str.split(t).length;

    return result-1;
}

let str = 'COMPUTERPROGRAMMING' ;
console.log(solution(str,'R'));

⇒ split() 메서드는 전달받은 인수를 기준으로 문자열을 자르는 것
⇒ 문자열을 자른 덩어리는 R의 갯수 + 1

profile
주니어 개발자의 기록

0개의 댓글