DEV Community

Kurapati Mahesh
Kurapati Mahesh

Posted on • Updated on

Find min length string in given array

Intended for beginners:

Input: ['Green', 'red', 'blue'];
Output: 'red' - Minimum length String.

function minLenString(arr) {
    return arr.reduce((a, b) => a.length <= b.length ? a : b);
}
Enter fullscreen mode Exit fullscreen mode

To find min length in array.

function minLenString(arr) {
    return Math.min(...arr.map(item => item.length));
}
Enter fullscreen mode Exit fullscreen mode

Thanks.

Top comments (2)

Collapse
 
jzombie profile image
jzombie

Two things I'm going to suggest here:

  • The Input array is formatted wrong (the color "red" string doesn't have an ending quote)
  • Both functions are named the exact same thing, while one returns a string, and the other a number.

If this is targeted for beginners, the goal would to be to make them as least confused as possible.

Collapse
 
urstrulyvishwak profile image
Kurapati Mahesh

Thanks @jzombie, Thatz a typo. I will update.