-
Notifications
You must be signed in to change notification settings - Fork 5
/
wordLib.js
48 lines (39 loc) · 1.17 KB
/
wordLib.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
var consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'];
var vowels = ['a', 'e', 'i', 'o', 'u'];
module.exports = function() {
return {
determinePattern: function(word) {
console.log(`Analyzing pattern of "${word}"...`);
var characters = word.split('');
var pattern = characters.map((character) => {
return this.determineCharacterType(character);
});
return pattern;
},
determineCharacterType: function(character) {
if (vowels.indexOf(character) !== -1) {
return 'v';
}
return 'c'
},
generateRandomWord(pattern) {
return pattern.map((character) => {
if (character === 'v') {
return this.generateRandomVowel();
}
return this.generateRandomConsonant();
}).join('');
},
generateRandomVowel() {
var vowel = this.randomItemFrom(vowels);
return vowel;
},
generateRandomConsonant() {
var consonant = this.randomItemFrom(consonants);
return consonant;
},
randomItemFrom(array) {
return array[Math.floor(Math.random() * array.length)];
},
}
}