मान लीजिए, हमें एक JavaScript फ़ंक्शन लिखना है जो एक स्ट्रिंग लेता है और शब्दों को उनकी बढ़ती लंबाई के अनुसार पुनर्व्यवस्थित करके एक नई स्ट्रिंग लौटाता है।
कोड
const str = 'This is a sample string only';
const arrangeByLength = str => {
const strArr = str.split(' ');
const sorted = strArr.sort((a, b) => a.length - b.length);
return sorted.join(' ');
};
console.log(arrangeByLength(str));
आउटपुट
a is This only sample string