Check for Anagram in 2 strings

前端 未结 4 1296
天命终不由人
天命终不由人 2021-01-26 19:16

I\'ve created a function that checks if 2 words are anagrams, but I want to make it better. I feel the declaration of the counter after in the if statement is not quite well, if

4条回答
  •  太阳男子
    2021-01-26 19:20

    function checkAnagram(string1, string2) {
       return string1.replace(/[^\w]/g,'').toLowerCase().split('').sort().join('') === 
            string2.toLowerCase().replace(/[^\w]/g,'').split('').sort().join('')
    }
    

提交回复
热议问题