저장을 습관화
프로그래머스 LV.0 0 떼기 본문
프로그래머스 LV.0 0 떼기
https://school.programmers.co.kr/learn/courses/30/lessons/181847
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 문제 명
0 떼기
2. 문제 설명
정수로 이루어진 문자열 n_str이 주어질 때, n_str의 가장 왼쪽에 처음으로 등장하는 0들을 뗀 문자열을 return하도록 solution 함수를 완성해주세요.
3. 제한 사항
- 2 ≤ n_str ≤ 10
- n_str이 "0"으로만 이루어진 경우는 없습니다.
4. 예시
n_str | result |
"0010" | "10" |
"854020" | "854020" |
5. 기본 제공 코드
function solution(n_str) {
var answer = '';
return answer;
}
6. 제출한 내 답
const solution = (n_str) => +n_str + "";
6-2. VSC에 작성한 내용
const solution = (n_str) => +n_str + "";
// 테스트
console.log(solution("0010"));
console.log(solution("00000854020"));
7. 특이사항
없음
8. 다른 사람이 작성한 답
8-1.
const solution = (str) => String(Number(str))
8-2. 정규표현식
function solution(n_str) {
return n_str.replace(/^0+/, '');
}
function solution(n_str) {
var answer = '';
return n_str.replaceAll(/^[0]+/g, '')
}
'코딩 테스트 > 프로그래머스 - 자바스크립트' 카테고리의 다른 글
프로그래머스 LV.0 n번째 원소부터 (0) | 2023.09.16 |
---|---|
프로그래머스 LV.0 홀수 vs 짝수 (0) | 2023.09.16 |
프로그래머스 LV.0 카운트 다운 (0) | 2023.09.16 |
프로그래머스 LV.0 공백으로 구분하기 2 (0) | 2023.09.16 |
프로그래머스 LV.0 공백으로 구분하기 1 (0) | 2023.09.16 |