हमें एक जावास्क्रिप्ट फ़ंक्शन लिखना है जो दो स्ट्रिंग्स में लेता है; पहली स्ट्रिंग के पहले 2 शब्दों के साथ एक नई स्ट्रिंग बनाता है और देता है, दूसरे स्ट्रिंग के अगले दो शब्द, फिर पहले, फिर दूसरे और इसी तरह।
उदाहरण के लिए:यदि तार हैं -
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