-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanagram-detection.js
More file actions
45 lines (29 loc) · 1.23 KB
/
anagram-detection.js
File metadata and controls
45 lines (29 loc) · 1.23 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
//6 Kyu
//Bouncing Balls
//Fundamentals, algorithms, mathematics
// 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"
//Solution
// write the function isAnagram
var isAnagram = function(test, original) {
//split original into an array of strings
let check = original.split('').toLowerCase()
//loop through the original str
for(let i=0; i<original.length; i++){
//check if an index exists of each element in parameter original
if(check.indexOf(test[i]) === -1){
//if one doesnt exhist return false
return false
}
//at the end of the loop all letters from parameter original exhists in test. Return true
return true
}
};
//str1 -> string of words, wont be empty, wont be null or undefined, will always be a str
//str2 -> string of words, wont be empty, wont be null or undefined, will always be a str
//boolean -> true if all the letters from test are in original, else false
console.log(isAnagram("foefet"), "toffee")