ปัญหา
เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่รับสตริงและตัวเลขเป็นสองอาร์กิวเมนต์
ฟังก์ชันของเราควรทำซ้ำสตริงอินพุตตามจำนวนครั้งที่ระบุโดยอาร์กิวเมนต์ที่สองและส่งคืนสตริงที่ทำซ้ำใหม่
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
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