একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা দুটি স্ট্রিং গ্রহণ করবে; প্রথম স্ট্রিংয়ের প্রথম ২টি অক্ষর, দ্বিতীয় স্ট্রিংয়ের পরবর্তী ২টি অক্ষর, আবার প্রথম স্ট্রিংয়ের ২টি, তারপর দ্বিতীয় স্ট্রিংয়ের ২টি — এইভাবে একসাথে মিক্স করে একটি নতুন স্ট্রিং তৈরি করবে এবং ফেরত দেবে।
উদাহরণস্বরূপ, যদি স্ট্রিংগুলো হয় −
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