From d5dc003ecc1459a4487282f5e71d6e331924a602 Mon Sep 17 00:00:00 2001 From: Savio Date: Sun, 30 Oct 2022 15:10:16 -0300 Subject: [PATCH] feat(isAnagram): Adicionando algoritmo que retorna se uma palavra e anagrama da outra. --- 08-isAnagram/index.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/08-isAnagram/index.js b/08-isAnagram/index.js index 918308a..c73b1e6 100644 --- a/08-isAnagram/index.js +++ b/08-isAnagram/index.js @@ -1,4 +1,22 @@ export function isAnagram(word1, word2) { - // implementar logica aqui - -} \ No newline at end of file + let word1Lower = Array.from(word1.toLowerCase()); + let word2Lower = Array.from(word2.toLowerCase()); + if (word1Lower.length !== word2Lower.length) { + return false; + } + let contador = 0; + while (word1.length !== contador) { + let letraword2 = word2Lower[0]; + if (word1Lower.indexOf(letraword2) !== -1) { + let indice = word1Lower.indexOf(letraword2); + word1Lower.splice(indice, 1); + word2Lower.shift(); + } + contador = contador + 1; + } + if (word1Lower.length === 0 && word2Lower.length === 0) { + return true; + } else { + return false; + } +}