N
N
Nastya Khabarova2021-06-25 21:23:10
JavaScript
Nastya Khabarova, 2021-06-25 21:23:10

How to find and capitalize the first letter of the shortest word in an array?

How to find and capitalize the first letter of the shortest word in an array? There is an implementation that capitalizes the longest word in an array, but how do you capitalize the shortest word?

class MyStr {
    ucMinWord(string) {
        let i=0;
        const wordArr=string.split(" ");
        const minString=string.substring(string.lastIndexOf(" "));
        for (i in wordArr)
        {
            if (wordArr[i].length>minString.length)
            {
                wordArr[i] = wordArr[i][0].toUpperCase() + wordArr[i].slice(1);
            }
        }
        return wordArr.join(' ')
    }
}
const app = new MyStr()
console.log(app.ucMinWord('deveveloper dev code'))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-06-25
@Karpenko_NASTYA

const index = arr.reduce((min, n, i, a) => a[min]?.length < n.length ? min : i, -1);
arr[index] = arr[index][0].toUpperCase() + arr[index].slice(1);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question