-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
isograms.js
57 lines (50 loc) · 1.2 KB
/
isograms.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
53
54
55
56
57
// isIsogram( "Dermatoglyphics" ) == true
// isIsogram( "aba" ) == false
// isIsogram( "moOse" ) == false // -- ignore letter case
function isIsogram(str) {
// a place to store the counts
const counts = {};
// iterate over the string
for (let i = 0; i < str.length; i++) {
// see if we have seen this letter before
const letter = str[i].toLowerCase();
if (!counts[letter]) {
// if not add it to the counts with a count of 1
counts[letter] = 1;
} else {
// else
// NOT AN ISOGRAM
return false;
}
}
// IS AN ISOGRAM
return true;
}
function isIsogram(str) {
// a place to store the counts
const counts = {};
// iterate over the string
return !str.split('').some((letter) => {
letter = letter.toLowerCase();
if (!counts[letter]) {
counts[letter] = 1;
return false;
} else {
return true;
}
});
}
function isIsogram(str) {
// a place to store the counts
const counts = {};
// iterate over the string
return !Array.prototype.some.call(str, (letter) => {
letter = letter.toLowerCase();
if (!counts[letter]) {
counts[letter] = 1;
return false;
} else {
return true;
}
});
}