forked from reactivepixel/Max-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrollerUtils.js
More file actions
29 lines (28 loc) · 842 Bytes
/
controllerUtils.js
File metadata and controls
29 lines (28 loc) · 842 Bytes
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
class controllerUtils {
constructor() {
// Method to generate random numeric verification code
// Modified to fit style guide from this SO answer:
// https://stackoverflow.com/a/39774334
this.generateCode = (n) => {
// Workaround method for Math.pow() and ** operator
const pow = (base, exp) => {
let result = 1;
for (let i = 0; i < exp; i += 1) {
result *= base;
}
return result;
};
const add = 1;
let max = 12 - add;
let min = 0;
if (n > max) {
return this.generateCode(max) + this.generateCode(n - max);
}
max = pow(10, n + add);
min = max / 10;
const number = Math.floor(Math.random() * (max - (min + 1))) + min;
return ('' + number).substring(add);
};
}
}
module.exports = controllerUtils;