개발로그필름
[프로그래머스/JS] 접두사인지 확인하기 본문
728x90
반응형
SMALL
https://school.programmers.co.kr/learn/courses/30/lessons/181906
💜나의 풀이
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
'coding test > 프로그래머스' 카테고리의 다른 글
[프로그래머스/js] 배열의 원소만큼 추가하기 (0) | 2024.04.19 |
---|---|
[프로그래머스/JS] 배열의 원소 삭제하기 (0) | 2024.04.19 |
[프로그래머스/JS] 자릿수 더하기 (0) | 2024.03.11 |
[프로그래머스/JS] n의 배수 고르기 (0) | 2024.03.07 |
[프로그래머스/JS] 배열의 유사도 (0) | 2024.03.06 |
Comments