저장을 습관화
프로그래머스 LV.0 접두사인지 확인하기 본문
프로그래머스 LV.0 접두사인지 확인하기
https://school.programmers.co.kr/learn/courses/30/lessons/181906
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 문제 명
접두사인지 확인하기
2. 문제 설명
어떤 문자열에 대해서 접두사는 특정 인덱스까지의 문자열을 의미합니다. 예를 들어, "banana"의 모든 접두사는 "b", "ba", "ban", "bana", "banan", "banana"입니다.
문자열 my_string과 is_prefix가 주어질 때, is_prefix가 my_string의 접두사라면 1을, 아니면 0을 return 하는 solution 함수를 작성해 주세요.
3. 제한 사항
- 1 ≤ my_string의 길이 ≤ 100
- 1 ≤ is_prefix의 길이 ≤ 100
- my_string과 is_prefix는 영소문자로만 이루어져 있습니다.
4. 예시
my_string | is_prefix | result |
"banana" | "ban" | 1 |
"banana" | "nan" | 0 |
"banana" | "abcd" | 0 |
"banana" | "bananan" | 0 |
5. 기본 제공 코드
function solution(my_string, is_prefix) {
var answer = 0;
return answer;
}
6. 제출한 내 답
const solution = (my_string, is_prefix) => {
return my_string.slice(0, is_prefix.length) === is_prefix ? 1 : 0;
};
6-2. VSC에 작성한 내용
const solution = (my_string, is_prefix) => {
return my_string.slice(0, is_prefix.length) === is_prefix ? 1 : 0;
};
// 테스트
console.log(solution("banana", "ban"));
console.log(solution("banana", "nan"));
console.log(solution("banana", "abcd"));
console.log(solution("banana", "bananan"));
7. 특이사항
없음
8. 다른 사람이 작성한 답
8-1. startWith 메소드 이런 것도 있었네
function solution(my_string, is_prefix) {
return +my_string.startsWith(is_prefix);
}
'코딩 테스트 > 프로그래머스 - 자바스크립트' 카테고리의 다른 글
프로그래머스 LV.0 조건에 맞게 수열 변환하기 2 (0) | 2023.09.15 |
---|---|
프로그래머스 LV.0 조건에 맞게 수열 변환하기 1 (0) | 2023.09.15 |
프로그래머스 LV.0 문자열 뒤의 n글자 (0) | 2023.09.14 |
프로그래머스 LV.0 길이에 따른 연산 (0) | 2023.09.14 |
프로그래머스 LV.0 삼각형의 완성조건 (1) (0) | 2023.09.14 |