How to find the length of the second smallest word in a string with Node.js

1 Answer

0 votes
const secondSmallestWordLength = str => {
    const arr = str.split(' ');
    
    if (arr.length < 2) {
        return false;
    }
    
    for (let i = 0; i < arr.length; i++) {
        arr[i] = arr[i].length;
    };
    
    arr.sort((a, b) => a - b);
    
    return arr[1];
};

const str = 'java c++ python c# node.js';

console.log(secondSmallestWordLength(str));




/*
run:

3

*/

 



answered Mar 21, 2024 by avibootz

Related questions

1 answer 97 views
1 answer 128 views
1 answer 123 views
2 answers 151 views
...