हमें एक JavaScript फ़ंक्शन लिखना है जो दो स्ट्रिंग्स लेता है, और एक नई स्ट्रिंग बनाता और लौटाता है जिसमें पहली स्ट्रिंग के पहले दो शब्द, दूसरी स्ट्रिंग के अगले दो शब्द, फिर पहली के, फिर दूसरी के, और इसी तरह हों।
उदाहरण
यदि स्ट्रिंग्स हैं:
const str1 = 'Hello world'; const str2 = 'How are you btw';
तो आउटपुट होना चाहिए:
const output = 'HeHollw o arwoe rlyodu btw';
कोड
const str1 = 'Hello world';
const str2 = 'How are you btw';
const twiceJoin = (str1 = '', str2 = '') => {
let res = '', i = 0, j = 0, temp = '';
for (let ind = 0; i < str1.length; ind++) {
if (ind % 2 === 0) {
temp = (str1[i] || '') + (str1[i+1] || '');
res += temp;
i += 2;
} else {
temp = (str2[j] || '') + (str2[j+1] || '');
res += temp;
j += 2;
}
}
while (j < str2.length) { res += str2[j++]; }
return res;
};
console.log(twiceJoin(str1, str2));
आउटपुट
HeHollw o arwoe rlyodu btw