-
Notifications
You must be signed in to change notification settings - Fork 5
/
run.js
38 lines (30 loc) · 1.28 KB
/
run.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
var WordLib = require('./wordLib.js');
var wordLibrary = new WordLib();
var inputString = String('My brain is a beautiful thing, and I intend to use it at WeCamp!');
console.log(`Doing stuff with string: "${inputString}" \n`);
var inputStringWithNoPunctuation = inputString.replace(/[!,]/g,'').toLowerCase();
var arrayOfWords = inputStringWithNoPunctuation.split(" ");
var totalProcessTime = 0;
var totalTries = 0;
var outputString = arrayOfWords.map((word) => {
var start = Date.now();
var wordPattern = wordLibrary.determinePattern(word);
var wordNotFound = true;
var i = 0;
console.log(`Trying to recreate "${word}" using generateRandomWord()...`);
while(wordNotFound) {
var randomWord = wordLibrary.generateRandomWord(wordPattern);
if (randomWord === word) {
wordNotFound = false;
}
i++;
}
var end = Date.now();
var processTime = (end - start);
console.log(`The word "${randomWord}" has been randomly generated after ${i} tries, process took ${processTime} ms...\n`);
totalTries += i;
totalProcessTime += processTime;
return randomWord;
});
console.log(`We've randomly created "${outputString.join(' ')}" from "${inputString}"`);
console.log(`This process took ${Math.floor(totalProcessTime/1000)} seconds and ${totalTries} random words guesses.`);