-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagram Detection.js
More file actions
56 lines (48 loc) · 1.28 KB
/
Anagram Detection.js
File metadata and controls
56 lines (48 loc) · 1.28 KB
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
53
54
55
56
// An anagram is the result of rearranging the letters of a word to produce a new word (see wikipedia).
// Note: anagrams are case insensitive
// Complete the function to return true if the two arguments given are anagrams of each other; return false otherwise.
// Examples
// "foefet" is an anagram of "toffee"
// "Buckethead" is an anagram of "DeathCubeK"
// write the function isAnagram
var isAnagram = function (test, original) {
test = test.toLowerCase().split('')
original = original.toLowerCase().split('')
while (original.length > 0) {
const f = original.indexOf(test[0])
if (f === -1) return false
test.splice(0, 1)
original.splice(f, 1)
}
return true
}
console.log(
isAnagram('foefet', 'toffee'),
true,
'The word foefet is an anagram of toffee'
)
console.log(
isAnagram('Buckethead', 'DeathCubeK'),
true,
'The word Buckethead is an anagram of DeathCubeK'
)
console.log(
isAnagram('Twoo', 'WooT'),
true,
'The word Twoo is an anagram of WooT'
)
console.log(
isAnagram('dumble', 'bumble'),
false,
'Characters do not match for test case dumble, bumble'
)
console.log(
isAnagram('ound', 'round'),
false,
'Missing characters for test case ound, round'
)
console.log(
isAnagram('apple', 'pale'),
false,
'Same letters, but different count'
)