-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassLatrelle.js
More file actions
executable file
·243 lines (204 loc) · 5.81 KB
/
classLatrelle.js
File metadata and controls
executable file
·243 lines (204 loc) · 5.81 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
Create a linked list.
*/
/*
This is an experimental feature branch for running in the NODE REPL.
It has testing flags down at the switch statement.
Once it works it can be merged with the browser-based version of this program.
*/
/*
_ _ _
(_) (_) | |
_ _ __ _ | |_ ___
| | | '_ \ | | | __| / __|
| | | | | | | | | |_ \__ \
|_| |_| |_| |_| \__| |___/
*/
// Lowest node must be findable, so
// make its ID 000001
let config = {};
config.nodes = {};
config.currentID = 1,
config.soonID = null,
config.usedIDs = []; // stores to ensure unique new IDs
/*_ _
| | (_)
| | ___ __ _ _ ___
| | / _ \ / _` | | | / __|
| | | (_) | | (_| | | | | (__
|_| \___/ \__, | |_| \___|
__/ |
|___/
*/
/*
** The two inputs in this process:
** NODE CONSTRUCTOR only takes a NEXTID
** But it contains a call to IDBESTOW which takes CONFIG.USEDIDS
**
** TODO Currently requires global: config.usedIDs; that seems sketchy. Maybe make that internal/private.
*/
/*
** Just takes a nextid
** returns an object of the Node CLASS
*/
class Node {
constructor(nextID){
this.first = nameBestow(),
this.phone = phoneBestow(),
this.instantiated = (new Date()).getMilliseconds(),
this.fwd = idBestow(config.usedIDs);
this.here = nextID;
}
/* This method is a getter to use immediately after
a new node is made, to update the config.SOONID in the globals
*/
getNextID() {
return this.nextID();
}
}
function nameBestow() {
const folks = ['Kisha', 'LaTrell', 'Tanifer', 'Terry', 'Moira', 'Grant', 'Ibrahim', 'Gump', 'Klondike', 'Treyvon', 'Eric', 'Sasha', 'Eleanor', 'Noah', 'Dolomite', 'Fernando', 'Caesar' ];
return folks[ Math.floor( 17 * Math.random() ) ]
}
function phoneBestow() {
let ac = [212, 415, 313, 608, 708, 917],
theirNumber = 0,
which = Math.floor(Math.random() * 6);
return Math.floor(Math.random() * 9999999) + ac[which] * 10000000;
}
// stores created IDs in ARR
// and accesses ARR to see if prior redundancy
function idBestow(arr) {
let fiveDigit = 0,
notUnique = true;
if (arr.length > 9999) {
return 'Maximum array size reached. All 9999 numbers have been dealt.'
}
while (notUnique) {
fiveDigit = Math.floor(Math.random() * 10000);
if (arr !== undefined ){
// array exists. check it. if result comes back
// TRUE, you've already used this number
notUnique = arr.some( (item)=> (item === fiveDigit)? true : false )
} else {
// Array of used IDs is empty, so...
notUnique = false
}
}
// store the new val
// return the newval
arr.push(fiveDigit);
return fiveDigit;
// todo For some reason this works great but also returns a superluous UNDEFINED at end, which is not a problem but is mysterious.
}
/*----T-E-S-T-E-R-S------------*/
// Accepts a person object.
// Returns nothing.
// Prints a phone number.
function sayPersonsPhone(po) {`Phone, within person-object is ${po.phone}`}
// Tests the function
function showFiveIds() {
let i,
stop;
for (i = 0, stop = 5; i < stop; i++) {
console.log(idBestow(config.usedIDs));
}
}
// Where n is the next ID
// Returns an object of type NODE
function testNodeConstructor( n ) {
let obj = new Node(n);
return obj;
}
// Accepts the next ID number
function make99(n) {
let i = 0,
stop = 99,
newNode = null,
nodeHoldingObject = {};
for (; i < stop; i++){
newNode = new Node(n);
console.log(`About to set next as ${newNode.fwd}`);
n = newNode.fwd;
nodeHoldingObject[newNode.here] = newNode
}
return nodeHoldingObject;
}
// Accepts the next ID number
// Accepts the number of nodes requested
function makeAny(n, stop) {
let i = 0,
newNode = null,
nodeHoldingObject = {},
finalNextID = null;
for (; i < stop; i++){
newNode = new Node(n);
console.log(`About to set next as ${newNode.fwd}`);
n = newNode.fwd;
nodeHoldingObject[newNode.here] = newNode
finalNextID = newNode.fwd;
finalHereID = newNode.here;
}
nodeHoldingObject[finalHereID].fwd = null;
// set a null for address of final node's nextID
return nodeHoldingObject;
}
// _
// (_)
// _ __ ___ __ _ _ _ __
// | '_ ` _ \ / _` | | | | '_ \
// | | | | | | | (_| | | | | | | |
// |_| |_| |_| \__,_| |_| |_| |_|
// Test mode options when run as node
if (process.argv[2] !== undefined) {
switch (process.argv[2]) {
case 'node':
let newNode = testNodeConstructor(config.soonID);
console.log(newNode.first);
console.log(newNode.phone);
console.log(`About to set next as ${newNode.fwd}`);
config.soonID = newNode.fwd;
break;
case 'id':
console.log(idBestow(config.usedIDs));
break;
case 'showfiveids':
console.log(showFiveIds());
break;
case '99':
let str1 = JSON.stringify(make99(config.soonID));
console.log(str1);
break;
case 'shownodeown':
let node88 = testNodeConstructor(88);
for (p in node88){
if (node88.hasOwnProperty(p)){
console.log(`====${p} is ${node88[p]}====`);
}
}
console.log('finished');
break;
case 'shownodeall':
let node77 = testNodeConstructor(77);
for (p in node77){
console.log(`====${p} is ${node77[p]}====`);
}
console.log('finished')
break;
case 'number':
let str2 = JSON.stringify(makeAny(config.soonID, process.argv[3]));
console.log(str2);
break;
case 'break':
let str3 = JSON.stringify(makeAny(config.soonID, process.argv[3]));
// TODO could use <br /> instead
str3 = str3.replace(/{/g , '</p><p>{ ');
str3 = str3.replace(/}/g , '}</p><p>');
str3 = '<p>' + str3 + '</p>'
console.log(str3);
break;
default:
console.log('no optional args recognized');
}
}
console.log( 'See the test switch for running options.' );