From 3ecfc6d775a98b83aef1e7c0ab9df1302d8c4ba6 Mon Sep 17 00:00:00 2001 From: guiprj Date: Sat, 29 Oct 2022 20:44:19 -0300 Subject: [PATCH] feat: resolucao da funcao longestWords --- 10-longestWords/index.js | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/10-longestWords/index.js b/10-longestWords/index.js index a98d2d8..4d26cd4 100644 --- a/10-longestWords/index.js +++ b/10-longestWords/index.js @@ -1,4 +1,36 @@ export function longestWords(words) { // implementar logica aqui - -} \ No newline at end of file + + let wordsInfo = [] + let newArr = [] + + + words.forEach(word => { + const wordInfo = { + [word]: word.length + } + wordsInfo.push(wordInfo) + }) + + let maxValue = 0 + + wordsInfo.forEach(wordInfo => { + let newMaxValue = Math.max(...Object.values(wordInfo)) + if (newMaxValue > maxValue) { + maxValue = newMaxValue + } + }) + + wordsInfo.forEach(wordInfo => { + Object.keys(wordInfo).filter((key) => { + if (key.length >= maxValue) { + newArr.push(key) + } + }) + }) + + return newArr + +} + +longestWords(["abacaxi", "melancia", "banana"]) // ["melancia"] \ No newline at end of file