-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanagram.js
52 lines (37 loc) · 1.23 KB
/
anagram.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
let anagramChecker = function (string1, string2) {
let passesChecks = string1.length == string2.length
if (passesChecks) {
let explode1 = explodeString(string1)
let explode2 = explodeString(string2)
let AllKeys1 = Object.keys(explode1)
let AllKeys2 = Object.keys(explode2)
let AllKeys = new Set(AllKeys1.concat(AllKeys2))
for (let i = 0; i < AllKeys.length; i++) {
if (!(AllKeys1.includes(AllKeys[i]) && AllKeys2.includes(AllKeys[i]))) {
passesChecks = false
break
}
}
AllKeys.forEach(key => {
if (!(explode1[key] == explode2[key])) passesChecks = false
})
}
console.log(passesChecks ? `${string1} is a anagram of ${string2}` : `${string1} is not a anagram of ${string2}`)
return passesChecks
}
function explodeString (stringValue) {
stringValue = stringValue.toLowerCase()
let letterStore = {}
for (let i = 0; i < stringValue.length; i++) {
if (letterStore.hasOwnProperty(stringValue.charAt(i))) {
letterStore[stringValue.charAt(i)]++
} else {
letterStore[stringValue.charAt(i)] = 1
}
}
return letterStore
}
anagramChecker('bob', 'obbo')
anagramChecker('bob', 'obo')
anagramChecker('bob', 'obb')
anagramChecker('bob', 'BOB')