Viết hàm nhận chuỗi, chỉ số bắt đầu và kết thúc. Tìm substring trong chuỗi được mở rộng vô hạn bằng cách lặp lại chuỗi gốc.
Ví dụ: str='helloo', start=12, end=15 → 'hel'.
Ví dụ
const str = 'helloo';
const start = 12;
const end = 15;
const findSubstring = (str = '', start, end) => {
let n = str.length;
let t = start / n;
start = start % n;
end -= t * n;
let res = str.substring(start, end - start);
if (end > n) {
t = (end - n) / n;
end = (end - n) - t * n;
while (t--) res += str;
res += str.substring(0, end);
}
return res;
};
console.log(findSubstring(str, start, end));
Kết quả
hel