-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathisAnagram.js
30 lines (22 loc) · 1 KB
/
isAnagram.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Code Wars
// isAnagram problem
// Write a function isAnagram to return true if the word test is an anagram of the word original and false if not
// Anagrams are case insensitive, ignore all non-alphanumeric characters, the input will always be two strings
function isAnagram(test, original) {
// strip out non-alphanumeric characters from both words [^A-Za-z0-9]
test = test.replace(/[^A-Za-z0-9]/g, '');
original = original.replace(/[^A-Za-z0-9]/g, '');
// convert words to all lowercase
test = test.toLowerCase();
original = original.toLowerCase();
// sort both words in lexicographical order
test = test.split('').sort().toString();
original = original.split('').sort().toString();
// compare whether both words are equal or not
return test === original;
}
// test
console.log(isAnagram("William Shakespeare","I am a weakish speller")); // true
console.log(isAnagram("Silent","Listen")); // true
console.log(isAnagram("Car","Cat")); // false
console.log(isAnagram("12345","54321")); // true