 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding second smallest word in a string - JavaScript
We are required to write a JavaScript function that takes in a string sentence as first and the only argument. The function should return the length of the second smallest word from the string.
For example: If the string is −
const str = 'This is a sample string';
Then the output should be 2.
Example
Following is the code −
const str = 'This is a sample string'; const secondSmallest = str => {    const strArr = str.split(' ');    if(strArr.length < 2){       return false;    }    for(let i = 0; i < strArr.length; i++){       strArr[i] = strArr[i].length;    };    strArr.sort((a, b) => a - b);    return strArr[1]; }; console.log(secondSmallest(str));  Output
Following is the output in the console −
2
Advertisements
 