เราต้องเขียนฟังก์ชัน JavaScript ที่รับสตริงเป็นอาร์กิวเมนต์แรก และดัชนีเริ่มต้นและสิ้นสุดเป็นอาร์กิวเมนต์ที่สองและสาม ฟังก์ชันต้องหาว่า ถ้าสตริงตัวแรกถูกขยายต่อเนื่องไปเรื่อยๆ โดยต่อท้ายด้วยสตริงเดิมซ้ำๆ สับสตริงที่ถูกล้อมรอบโดยดัชนีเริ่มต้นและสิ้นสุดจะเป็นอะไร
ตัวอย่าง
ถ้าสตริงและดัชนีเป็น:
const str = 'helloo'; const start = 11; const end = 15;
ผลลัพธ์ควรเป็น:
const output = 'hel';
ตัวอย่างโค้ด
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));
ผลลัพธ์
hel