ปัญหา
เราต้องเขียนฟังก์ชัน JavaScript ที่รับสตริงและตัวเลข 2 อาร์กิวเมนต์ ฟังก์ชันควรทำซ้ำสตริงเข้า จำนวนครั้งที่ระบุโดยอาร์กิวเมนต์ที่สอง และคืนค่าสตริงที่ทำซ้ำแล้ว
ตัวอย่าง
โค้ดตัวอย่างดังนี้:
const str = 'this';
const num = 8;
const repeatSpecificTimes = (str = '', num = 1) => {
const arr = [str];
let i = 1;
while(i < num){
arr.push(arr[0]);
i++;
};
return arr.join('');
};
console.log(repeatSpecificTimes(str, num));
ผลลัพธ์
thisthisthisthisthisthisthisthis