เราต้องเขียนฟังก์ชันที่รับสองอาร์กิวเมนต์: สตริงและตัวเลข ความยาวสตริงจะน้อยกว่าหรือเท่ากับตัวเลขเสมอ เราต้องเติมตัวอักษรพิมพ์เล็กสุ่มท้ายสตริงจนความยาวเท่ากับตัวเลขและคืนค่าสตริงใหม่
ตัวอย่าง
โค้ดสำหรับฟังก์ชันนี้:
const padString = (str, len) => { if(str.length < len){ const random = Math.floor(Math.random() * 26); const randomAlpha = String.fromCharCode(97 + random); return padString(str + randomAlpha, len); }; return str; }; console.log(padString('abc', 10)); console.log(padString('QWERTY', 10)); console.log(padString('HELLO', 30)); console.log(padString('foo', 10));ผลลัพธ์
ผลลัพธ์ในคอนโซล:
abckoniucl QWERTYcwaf HELLOdnulywbogqhypgmylqlvmckhg Foofhfnhon