-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerationScript.js
More file actions
117 lines (67 loc) · 2.63 KB
/
Copy pathgenerationScript.js
File metadata and controls
117 lines (67 loc) · 2.63 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/// BROKEN OUT FUNCTIONS
// Function replaces character in progress string if a match is found
// between the random string and the target string.
function setCharAt(str,index,chr) {
if(index > str.length-1) return str;
return str.substr(0,index) + chr + str.substr(index+1);
}
// Function creates a target string for each trial.
function createTargetString(){
targetString="";
for ( var n=0; n < targetLength; n++ ) {
var rnum = Math.floor(Math.random() * possibleCharacters.length);
targetString += possibleCharacters.substring(rnum,rnum+1);
}
return targetString;
}
// Function creates a random string to compare to the target string
// for each generation.
function createRandomString(){
randomString = "";
for ( var i=0; i < targetString.length; i++ ) {
var rnum = Math.floor(Math.random() * possibleCharacters.length);
randomString += possibleCharacters.substring(rnum,rnum+1);
}
return randomString;
}
// Function creates the progress string of empty characters
function createProgressString(){
var progressString = "";
for (var c=0; c < targetString.length; c++) {
progressString = progressString + "*";
}
return progressString;
}
// Function loops through target string and compares each character
// to the random string. Replaces character on progress string if match.
function loopCheckReplace(){
for (var p = 0; p < targetString.length; p++) {
var first_char = randomString.slice(p, p+1);
var second_char = targetString.slice(p, p+1);
if (first_char === second_char) {
progressString = setCharAt(progressString, p, second_char);
}
}
}
// Variable sets the characters that are used to generate the target
// string and also the random string for each generation.
var possibleCharacters = " abcdefghiklmnopqrstuvwxyz";
// Variable specifies the length of the target string.
var targetLength = 40;
targetString = createTargetString();
// for (var r = 0; r < numberOfTrials; r++){
var counter = 0;
var progressString = createProgressString();
// Trial Loop continues until progress string equals the target string.
do {
var counter = counter + 1;
var randomString = createRandomString();
loopCheckReplace();
console.log("GENERATION " + counter);
console.log("Target: " + targetString);
console.log("Random: " + randomString);
console.log("Progre: " + progressString);
console.log("");
} while (targetString != progressString);
// generationsRequiredResults[r] = counter;
// }