
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
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
- Related Questions & Answers
- Function to find the length of the second smallest word in a string in JavaScript
- Largest and smallest word in a string - JavaScript
- Finding shortest word in a string in JavaScript
- Finding the length of second last word in a sentence in JavaScript
- Finding the longest word in a string in JavaScript
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
- Finding the smallest multiple in JavaScript
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- JavaScript Recursion finding the smallest number?
- Finding smallest number using recursion in JavaScript
- Finding the smallest fitting number in JavaScript
- Finding the smallest good base in JavaScript
- Finding the second most frequent character in JavaScript
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- Finding all valid word squares in JavaScript
Advertisements