개발로그필름

[프로그래머스/JS] 접두사인지 확인하기 본문

coding test/프로그래머스

[프로그래머스/JS] 접두사인지 확인하기

yuullog 2024. 3. 27. 23:51
728x90
반응형
SMALL

https://school.programmers.co.kr/learn/courses/30/lessons/181906

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

💜나의 풀이 

function solution(my_string, is_prefix) {
    const is_prefix_len = is_prefix.length
    if(my_string.slice(0,is_prefix_len) === is_prefix){
        return 1
    } else{
        return 0
    }
}

• is_prefix의 전체 길이를 구한 후 접두사는 앞에 있는 단어니까 slice() 함수를 사용해 비교하였다

 

💜다른 사람 풀이 

function solution(my_string, is_prefix) {
  return +my_string.startsWith(is_prefix);
}

1. boolean 앞에 + 하면 true는 1, false는 0 반환

2. string.startsWith() : 어떤 문자열이 특정 문자로 시작하는지 확인해 결과를 true 또는 false 반환

* startsWith(string, position) position을 사용해 특정 위치부터 찾을 수도 있다

반응형
LIST
Comments