challenge-algorithms-v2.0-M.../10-longestWords/index.js

32 lines
1.1 KiB
JavaScript

export function longestWords(words) {
// implementar logica aqui
let str = words;
let longest = "";
let cont = 1
let maioresPalavras = 0
let mostra = "";
for (var word of str) {
if (word.length > longest.length){
cont++
longest = word
}else {
if (cont > maioresPalavras) {
maioresPalavras = cont;
mostra = longest;
}
cont = 1;
}
return mostra;
}
}
console.log(longestWords(["abacaxi", "melancia", "banana"]) "abacaxi", "melancia", "banana"); // melancia
console.log(longestWords(["aba", "aa", "ad", "vcd", "aba"]) "aba", "aa", "ad", "vcd", "aba"); // aba, vcd, aba
console.log(longestWords(["aa"]) "aa"); // aa
console.log(longestWords(["abc", "eeee", "abcd", "dcd"]) "abc", "eeee", "abcd", "dcd"); // eeee, abcd, abc
console.log(longestWords(["aa", "bb", "cc"]) "aa", "bb", "cc"); // aa, bb, cc
console.log(longestWords(["a", "abc", "cbd" ,"zzzzzz", "a", "abcdef", "asasa", "aaaaaa"]) "a", "abc", "cbd" ,"zzzzzz", "a", "abcdef", "asasa", "aaaaaa"); // zzzzzz, abcdef, aaaaaa