
문자열 문제를 풀 때, 햇갈리는 메서드 두개가 있어 정리해보고자 합니다.
str.substring(indexStart[, indexEnd 앞])
str.slice(beginIndex[, endIndex 앞])
const str = 'He110W0r1d';
console.log(str.substring(7, 2)); // output: "110W0"
console.log(str.slice(7, 2)); // output: ""
const str = 'He110W0r1d';
console.log(str.substring(-7, 2)); // output: "He"
console.log(str.substring(2, -5)); // output: "He"
console.log(str.slice(-7, 2)); // output: ""
console.log(str.slice(2, -5)); // output: "110"
문자열 my_string과 정수 n이 매개변수로 주어질 때, my_string의 뒤의 n글자로 이루어진 문자열을 return 하는 solution 함수를 작성해 주세요.
function solution(my_string, n) {
return my_string.slice(-n);
}