เราต้องเขียนฟังก์ชันที่รับสองสตริง ฟังก์ชันควรคืนค่าอาร์เรย์ใหม่ที่มีตัวอักษรสลับกันจากทั้งสองสตริง
ตัวอย่าง: หากสองสตริงคือ:
const str1 = 'abc'; const str2 = 'def';
ผลลัพธ์
ผลลัพธ์ควรเป็น:
const output = 'adbecf';
ตัวอย่าง
โค้ดสำหรับนี้:
const str1 = 'abc'; const str2 = 'def'; const mergeAlternatively = (str1, str2) => { const a = str1.split('').filter(el => !!el); const b = str2.split(''); let mergedString = ''; for(var i = 0; i < a.length || i < b.length; i++){ if(i < a.length){ mergedString += a[i]; }; if(i < b.length){ mergedString += b[i]; }; }; return mergedString; }; console.log(mergeAlternatively(str1, str2));ผลลัพธ์
ผลลัพธ์ในคอนโซล:
adbecf