코딩 테스트 풀이 🛠

[프로그래머스] 문자열 정렬하기 (1)

엄성준 2022. 12. 2. 23:55

문제 설명

 

문자열 my_string이 매개변수로 주어질 때, my_string 안에 있는 숫자만 골라 오름차순 정렬한 리스트를 return 하도록 solution 함수를 작성해보세요.

 

 

입출력 예

 

my_string result
"hi12392" [1, 2, 2, 3, 9]
"p2o4i8gj2" [2, 2, 4, 8]
"abcde0" [0]

 

 

나의 풀이

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function solution(my_string) {
    let answer = [];
    
    for(let i=0; i<my_string.length; i++){
        
        if(isNaN(my_string[i])===false){
            answer.push(Number(my_string[i]));
            
            answer.sort(function(a,b){
                return a-b;
            })
        }
    }
    return answer;
}
cs

 

문제를 풀며 느낌 점

 

- 매일 매일 화이팅!