I need to create a javascript script that given two strings checks if one is the anagram of the other, I thought about using a check with support arrays but I don’t know how to do it
>Solution :
Well,
- Learn JS.
- Practice LeetCode DAILY.
For now, I hope this helps.
const checkForAnagram = (str1, str2) => {
// create, sort, then join an array for each string
const aStr1 = str1.toLowerCase().split('').sort().join('');
const aStr2 = str2.toLowerCase().split('').sort().join('');
return aStr1 === aStr2;
}
console.log(`The strings are anagrams: ${checkForAnagram('hello', 'ehlol')}`);
console.log(`The strings are anagrams: ${checkForAnagram('cat', 'rat')}`);