-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
36 lines (31 loc) · 782 Bytes
/
index.js
File metadata and controls
36 lines (31 loc) · 782 Bytes
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
31
32
33
34
35
36
/**
* Returns true or false based on if two strings are anagrams
*
* @param {string} str The main word / phrase
* @param {number} cmp The word / phrase to compare
* @return {boolean} if the two words / phrases are anagrams
*/
module.exports = function isAnagram(str, cmp) {
if (typeof str !== 'string' || typeof cmp !== 'string') {
return false;
}
const mainWord = str
.toLowerCase()
.split('')
.sort();
const cmpWord = cmp
.toLowerCase()
.split('')
.sort();
if (mainWord.length !== cmpWord.length) {
return false;
}
let i = 0;
while (i < mainWord.length) {
if (mainWord[i] !== cmpWord[i]) {
return false;
}
i++;
}
return true;
};