If you want to pass the words as parameters to the function (as in your example: findLongestWord("The", "quick", "brown")
), then one option is to use Rest Parameters:
function findLongestWord(...words) {
var longest = "";
for (var word of words) {
if (word.length > longest.length) longest = word;
}
return longest;
}
console.log(findLongestWord("The", "quick", "brown")); // quick
Note the function statement I used ...words
to indicate that the number of parameters is variable, and all of them will be placed in words
. In the case, words
will already be an array, and therefore need not use split
or anything like that, just walk through it with for
.
And you also don’t need to sort the array: just scroll through it and go checking the size of each string, to find the largest. Sorting has an O(n log n), while traversing the array in a for
simple has cost O(n). Sorting the array seems completely unnecessary to me, since you just want to grab the biggest string, and whatever the other (see here a comparison and see how ordering the array makes it slower).
In the end, return the string itself, and not its size (I understood that you want the string, so do not return the length
yes to the string itself longest
).
Remember that in the case of a tie, the first string found is returned (in the example above, both "Quick" and "Brown" are the largest, and in this case, "Quick" was returned by being in the array at a position prior to "Brown").
On older browsers, not ES6 compliant, the Rest Parameters may not be available. In this case, another alternative is to use arguments
, which is an object array-like containing all arguments received by the function:
function findLongestWord(){
var longest = "";
for (var word of arguments) {
if (word.length > longest.length) longest = word;
}
return longest;
}
console.log(findLongestWord("The", "quick", "brown")); // quick
Passing an array
In the comments it was said that you want to pass an array with words to the function. In this case, you would need a small modification:
function findLongestWord(words) { // words agora é um array
var longest = "";
for (var word of words) {
if (word.length > longest.length) longest = word;
}
return longest;
}
console.log(findLongestWord([ "The", "quick", "brown" ])); // quick
Now, when calling the function, I passed [ "The", "quick", "brown" ]
- note the brackets, they indicate that this is an array (in this case, with 3 elements: the strings "The"
, "quick"
and "brown"
). Now the function takes a single parameter, which is the array containing the words.
This is different from findLongestWord("The", "quick", "brown")
<- in this case, you are passing 3 parameters to the function (3 separate strings), not an array containing the 3 strings.
It may seem like a silly detail, but it makes a difference if the array is in a variable. See:
function recebeArray(words) {
var longest = "";
for (var word of words) {
if (word.length > longest.length) longest = word;
}
return longest;
}
function recebeRestParams(...words) {
var longest = "";
for (var word of words) {
if (word.length > longest.length) longest = word;
}
return longest;
}
// array com as palavras
let palavras = [ "The", "quick", "brown" ];
console.log(recebeArray(palavras)); // quick
console.log(recebeRestParams(palavras)); // não funciona
// para funcionar, precisa aplicar o spread operator no array
console.log(recebeRestParams(...palavras)); // quick
To better understand about the Operator spread, read here.
The way you implemented it is passing 3 parameters to the function. As you said together I understand that you would like to pass an array of words as a function parameter. That’s it?
– Danizavtz
Yes, I would like to pass an array with words like I left an example in the console.log ("the", "Quick", "Brown") and hence a function that returns "Brown" only
– Michelle Mendonça
I personally I think it is not a semantically correct idea to use function with varied arity in this type of situation (several arguments). I think it is more ideal to pass a single array in the first (and only) argument. It would also facilitate the application of values that are already in the array format, without having to use spread or
apply
in the invocation...– Luiz Felipe