समस्या
हमें एक JavaScript फ़ंक्शन लिखना है जो दो स्ट्रिंग्स, str1 और str2 लेता है। हमारा फ़ंक्शन true लौटाना चाहिए यदि str1 के कैरेक्टर्स के एक हिस्से को रीअरेंज करके str2 से मिलान किया जा सकता है, अन्यथा false लौटाना चाहिए।
उदाहरण
निम्नलिखिक कोड है −
const str1 = 'rkqodlw'; const str2 = 'world'; const canForm = (str1 = '', str2 = '') => { if(str1.length < str2.length){ return false; }; const res = str2.split(''); str1.split("").forEach(val => { if(res.includes(val)){ res.splice(res.indexOf(val), 1); }; }); return res.length === 0; }; console.log(canForm(str1, str2));आउटपुट
कंसोल आउटपुट निम्नलिखिक है −
true