From d66c42f762a6998e6064a5ea33849bc56bc057ed Mon Sep 17 00:00:00 2001 From: MateusLopes Date: Wed, 2 Nov 2022 17:52:04 -0300 Subject: [PATCH] feat(isAnagram): algoritmo concluido --- 08-isAnagram/index.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/08-isAnagram/index.js b/08-isAnagram/index.js index 918308a..1cb593a 100644 --- a/08-isAnagram/index.js +++ b/08-isAnagram/index.js @@ -1,4 +1,17 @@ export function isAnagram(word1, word2) { - // implementar logica aqui - -} \ No newline at end of file + // implementar logica aqui + //Removendo espaços e deixando no mesmo formato + word1 = word1.replace(/\s/g, '').toLowerCase() + word2 = word2.replace(/\s/g, '').toLowerCase() + + // Se não tiver o mesmo tamanho não é anagrama + if (word1.length !== word2.length) { + return false + } + + word1 = word1.split('').sort().join('') + word2 = word2.split('').sort().join('') + + let result = word1 === word2 + return result +}