-
Notifications
You must be signed in to change notification settings - Fork 1
/
pangram-checker.js
58 lines (47 loc) · 1.61 KB
/
pangram-checker.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
58
'use strict';
// See index.html for context.
class PangramChecker {
constructor() {
this.ALPHA_ARRAY = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
this.FREQ_ARRAY = ["e","o","t","h","a","s","n","i","r","d","l","u","y","m","w","f","g","c","b","p","k","v","j","q","x","z"];
this.SCRAMBLE_ARRAY = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
this.letters_set = new Set();
for (let c of this.ALPHA_ARRAY) {
this.letters_set.add(c)
}
this.letters_array = [...this.ALPHA_ARRAY]
}
alphabetize() {
this.letters_array = [...this.ALPHA_ARRAY]
}
// Credit: https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
arrangeByFrequency() {
this.letters_array = [...this.FREQ_ARRAY]
}
scramble() {
this.letters_array = [...this.SCRAMBLE_ARRAY]
this.shuffleArray(this.letters_array)
}
getCounts(text) {
const lettersPresentSet = new Set();
for (let c of text.toLowerCase()) {
lettersPresentSet.add(c);
}
const lettersPresentVec = [];
const lettersAbsentVec = [];
for (let c of this.letters_array) {
if (lettersPresentSet.has(c)) {
lettersPresentVec.push(c)
} else {
lettersAbsentVec.push(c)
}
}
return {present: lettersPresentVec, absent: lettersAbsentVec}
}
}