프로그래머스 코딩테스트_2

cho·2023년 11월 28일

Question

예제 1번의 my_string에서 인덱스 2부터 overwrite_string의 길이만큼에 해당하는 부분은 "11oWor1"이고 이를 "lloWorl"로 바꾼 "HelloWorld"를 return 합니다.

my_stringoverwrite_stringsresult
"He11oWor1d""lloWorl"2"HelloWorld"
"Program29b8UYP""merS123"7"ProgrammerS123"
function solution(my_string, overwrite_string, s) {
    var answer = '';
    return my_string.slice(0, s) + overwrite_string + my_string.slice(overwrite_string.length+s);
}

-> slice 메서드 사용
The slice() method of Array instances returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.
배열의 일부를 선택하여 새로운 배열 객체에 대한 얕은 복사본을 반환한다. 여기서 'start' 와 'end'는 배열 내 항목의 인덱스를 나타낸다. 선택된 부분은 시작 인덱스부터 끝 인덱스 전까지다. (끝 인덱스는 포함되지 않는다.) 이 메서드를 사용하면 원본배열은 수정되지 않는다.

my_string.slice(0,s) -> my_string을 slice메서드로 잘라서 0번부터 s번 전까지 자른다.
my_string.slice(s + overwrite_string.length) -> s부터 overwrite_string의 길이만큼

0개의 댓글