Check if two strings are anagram - Javascript:
//function to check if two strings are anagram
function isAnagram(str1, str2) {
// convert both strings to lowercase
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
// create an array of characters from the first string
const arr1 = str1.split("");
// create an array of characters from the second string
const arr2 = str2.split("");
// sort the arrays
arr1.sort();
arr2.sort();
// compare the arrays
if (arr1.toString() === arr2.toString()) {
return true;
} else {
return false;
}
}
//example
const result = isAnagram("listen", "silent");
//output
console.log(result); //true
Top comments (0)