-
[프로그래머스 level.1] join(), split()알고리즘 2023. 6. 21. 11:01
숫자문자열과 영단어
function solution(s) { let numbers = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]; var answer = s; for(let i = 0; i < numbers.length; i++) { let arr = answer.split(numbers[i]); console.log(arr) answer = arr.join(i); console.log(answer) } return Number(answer); }
spit()는 받은 인자를 기준으로 하여 문자열을 나누기때문에 여기서 사용하게 된다면 문자열을 2개로 만들면서 기준이 되는 문자열을 없앨 수 있다. 이때 같이 쓴 join()는 받은 인자를 기준으로 하여 문자열을 합치기 때문에 split()에서 없앴던 문자 자리에 새롭게 내가 넣고싶은 문자를 추가할 수 있게 된다. 그렇게 해서 이러한 결과가 나오게 되었다;; 소름이네
'알고리즘' 카테고리의 다른 글
[프로그래머스 알고리즘] 1~N까지 배열만들기, 제곱근의 갯수와 약수의 관계 (0) 2023.06.21 [프로그래머스 level.1] 3중반복문, 소수 판별식 (0) 2023.06.21 [프로그래머스 level.1] 내림차순정렬, 아스키코드 변환 (0) 2023.06.21 [프로그래머스 level.1] slice 스파르타 예습 (0) 2023.06.20 [프로그래머스 level.1] filter , includes (0) 2023.06.20