From ccc77a00ddbf35c89f7bc6ffa2abb0f792526bee Mon Sep 17 00:00:00 2001 From: Ramon Dias Ferreira Date: Wed, 2 Nov 2022 16:39:30 -0300 Subject: [PATCH] =?UTF-8?q?refactor(mostUsedChar):=20codigo=20com=20logica?= =?UTF-8?q?=20toda=20refeita.=20Tendo=20como=20principais=20fun=C3=A7?= =?UTF-8?q?=C3=B5es=20split,=20forEach=20e=20o=20if.=20funcional=20em=20to?= =?UTF-8?q?dos=20os=20testes.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 09-mostRepeatedChar/index.js | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/09-mostRepeatedChar/index.js b/09-mostRepeatedChar/index.js index c7a2eb3..e95466a 100644 --- a/09-mostRepeatedChar/index.js +++ b/09-mostRepeatedChar/index.js @@ -1,19 +1,11 @@ export function mostUsedChar(text) { - // implementar logica aqui - - const map = new Map(); - - // Percorre as letras do texto - for (let letter of text) { - - // Busca a quantidade de vezes que a letra já se repetiu, ou 0 para a primeira ocorrência - let count = map.get(letter) || 0; - - // Atualiza a frequência incrementando-a - map.set(letter, count+1) - - console.log(map) - - } - - } \ No newline at end of file + let max = 0, + maxChar = ''; + text.split('').forEach(function(char){ + if(text.split(char).length > max) { + max = text.split(char).length; + maxChar = char; + } + }); + return maxChar; +} \ No newline at end of file