코딩테스트

코딩테스트연습 > 연습문제 > 서울에서 김서방 찾기

안양사람 2020. 7. 3. 21:55
728x90
SMALL

풀이

기본 for, if문

function solution(seoul) {
    for(let i=0;i<seoul.length;i++){
        if(seoul[i]==="Kim"){
            return "김서방은 "+i+"에 있다"
            break;
        }
    }
}

 indexof 사용

function solution(seoul) {
    const i=seoul.indexOf('Kim');
    return "김서방은 "+i+"에 있다"
}
728x90
LIST