How to find the length of the second smallest word in a string with TypeScript

1 Answer

0 votes
const secondSmallestWordLength = (str: string) => {
    const arr: any[] = 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: string = 'java c++ python c# typescript';

console.log(secondSmallestWordLength(str));



/*
run:

3

*/

 



answered Mar 21, 2024 by avibootz

Related questions

...