JS - Write a function that returns the largest STRING [closed]

-4
  • Write a function that returns the largest given string.
  • example:

    entry: "StackOverflow" "No response" "Tags"

    Output:

    "StackOverflow" // string with the largest number of characters.

        
    asked by anonymous 21.12.2015 / 12:32

    1 answer

    5

    You do not need to write a function that already exists, use Array. prototype.reduce ()

    var strings = ["StackOverflow", "Sem resposta", "Tags"];
    var retorno = strings.reduce(function (atual, proximo) {
      return atual.length > proximo.length ? atual : proximo;
    });
    alert(retorno);
        
    21.12.2015 / 12:58