From 6e7bf888af841d9e13b26437f39ec782fafe1a09 Mon Sep 17 00:00:00 2001 From: Nathalia Sardou Date: Mon, 31 Oct 2022 17:29:04 -0300 Subject: [PATCH] isAnagram atualizado --- 08-isAnagram/index.js | 13 +++++++++++-- 09-mostRepeatedChar/index.js | 31 +++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/08-isAnagram/index.js b/08-isAnagram/index.js index 918308a..84fb936 100644 --- a/08-isAnagram/index.js +++ b/08-isAnagram/index.js @@ -1,4 +1,13 @@ export function isAnagram(word1, word2) { // implementar logica aqui - -} \ No newline at end of file + if (word1.length !== word2.length) { + return false; + } + + var str1 = word1.toLowerCase().toUpperCase().split('').sort().join(''); + var str2 = word2.toLowerCase().toUpperCase().split('').sort().join(''); + if (str1 === str2) { + return true; + } + return false; +} diff --git a/09-mostRepeatedChar/index.js b/09-mostRepeatedChar/index.js index b113ed8..f29f390 100644 --- a/09-mostRepeatedChar/index.js +++ b/09-mostRepeatedChar/index.js @@ -1,4 +1,27 @@ -export function mostUsedChar(text) { - // implementar logica aqui - return "" -} \ No newline at end of file +function mostUsedChar(text) { + // implementar logica aqui + text = text.split('').sort(); + let cont1 = 0; + let cont2 = 0; + let maior = ''; + + for (let i = 1; i < text.length; i++) { + if (text[i] != text[i - 1]) { + if (cont2 > cont1) { + cont1 = cont2; + maior = text[i - 1]; + } + cont2 = 1; + } else { + cont2++; + } + } + + return maior; +} + +// Resultados esperados +console.log(mostUsedChar('fdgdfgff'), 'f'); // f +console.log(mostUsedChar('Lorem ipsum'), 'm'); // m +console.log(mostUsedChar('adsassdasd'), 's'); // s +console.log(mostUsedChar('testeeeee'), 'e'); // e