-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisograms-7kyu.js
30 lines (26 loc) · 1.05 KB
/
isograms-7kyu.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
// CODEWARS 7KYU -ISOGRAMS
// INSTRUCTIONS:
// An isogram is a word that has no repeating letters, consecutive or non-consecutive. Implement a function that determines whether a string that contains only letters is an isogram. Assume the empty string is an isogram. Ignore letter case.
// isIsogram("Dermatoglyphics") == true
// isIsogram("aba") == false
// isIsogram("moOse") == false // -- ignore letter case
// SOLUTION:
function isIsogram(str){
str = str.toLowerCase()
for (let i=0; i< str.length; i++){
for (let j= i+1; j<str.length; j++){
if(str[i] === str[j]){
return false;
}
}
}
return true;
}
// SOLUTION WITH REGEX:
//\w matches any word character (equivalent to [a-zA-Z0-9_])
// . matches any character (except for line terminators)
// * matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
// \1 matches the same text as most recently matched by the 1st capturing group
function isIsogram(str){
return !/(\w).*\1/i.test(str)
}