Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to create a script that given two strings prints true if one is an anagram of the other

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Well,

  1. Learn JS.
  2. 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')}`);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading