diff --git a/01-js/easy/anagram.js b/01-js/easy/anagram.js index fff61427..015bfbe0 100644 --- a/01-js/easy/anagram.js +++ b/01-js/easy/anagram.js @@ -8,7 +8,19 @@ */ function isAnagram(str1, str2) { - + str1 = str1.replace(/[^a-zA-Z0-9]/g, '').toLowerCase(); + str2 = str2.replace(/[^a-zA-Z0-9]/g, '').toLowerCase(); + if (str1.length !== str2.length) { + return false; + } + return str1.split('').sort().join('') === str2.split('').sort().join(''); } +console.log("Test case: spar vs rasp - ", isAnagram("spar", "rasp") ? "true" : "false"); +console.log("Test case: hello vs world - ", isAnagram("hello", "world") ? "true" : "false"); +console.log("Test case: Debit Card vs Bad Credit - ", isAnagram("Debit Card", "Bad Credit") ? "true" : "false"); +console.log("Test case: Astronomer vs Moon starer - ", isAnagram("Astronomer", "Moon starer") ? "true" : "false"); + + + module.exports = isAnagram;