samedi 18 juillet 2020

How can I return the message "this is empty" from a function when the input is an empty array?

I've written a function in javascript for finding the shortest string within an array. I want the function to return empty array when the input is an empty array. How can I do that ? Here is my code:

function tinyFriend(array) {
    var tiny = array[0];
  
    for (i = 0; i < array.length; i++) { 
      if (array[i].length < tiny.length) {
         tiny = array[i];
      } 
    }
    
    return tiny;
}

Aucun commentaire:

Enregistrer un commentaire