हमें एक जावास्क्रिप्ट फंक्शन लिखना है जो एक स्ट्रिंग ले और उससे सबसे छोटा शब्द वापस करे।
उदाहरण के लिए: यदि इनपुट स्ट्रिंग है:
const str = 'This is a sample string';
तो आउटपुट होना चाहिए:
const output = 'a';
उदाहरण
इसके लिए कोड:
const str = 'This is a sample string'; const findSmallest = str => { const strArr = str.split(' '); const creds = strArr.reduce((acc, val) => { let { length, word } = acc; if(val.length < length){ length = val.length; word = val; }; return { length, word }; }, { length: Infinity, word: '' }); return creds.word; }; console.log(findSmallest(str));आउटपुट
कंसोल में आउटपुट:
a