JS - Write a function that returns the largest STRING

Asked

Viewed 219 times

-4

  1. Write a function that returns the largest string given.

example:

entree: "Stackoverflow" "No Response" "Tags"

exit:

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

  • 4

    what have you done to try to get to it? Show your code if possible

  • 1

    Sometimes a simple google search returns the answer.

1 answer

5

You don’t 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.