हमें एक JavaScript फ़ंक्शन लिखना है जो दो स्ट्रिंग्स लेता है जिनमें कुछ सामान्य तत्व हो सकते हैं या नहीं। फ़ंक्शन को एक खाली स्ट्रिंग लौटानी चाहिए यदि कोई सामान्य तत्व मौजूद नहीं है, अन्यथा दोनों स्ट्रिंग्स के बीच सभी सामान्य तत्वों वाली एक स्ट्रिंग।
हमारी दो स्ट्रिंग्स निम्नलिखित हैं:
const str1 = 'Hey There!!, how are you'; const str2 = 'Can this be a special string';
कोड
const str1 = 'Hey There!!, how are you';
const str2 = 'Can this be a special string';
const commonString = (str1, str2) => {
let res = '';
for (let i = 0; i < str1.length; i++) {
if (!str2.includes(str1[i])) { continue; }
res += str1[i];
}
return res;
};
console.log(commonString(str1, str2));
आउटपुट
e here h are