-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
43 lines (35 loc) · 1003 Bytes
/
functions.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
//Various functions
function mathRandomInt(a, b) {
if (a > b) {
// Swap a and b to ensure a is smaller.
var c = a;
a = b;
b = c;
}
return Math.floor(Math.random() * (b - a + 1) + a);
}
function shuffleAllNumsTo(num) {
var array = [];
for (var i = 0; i < num; i++) {
array.push(i + 1);
}
var currentIndex = array.length, randomIndex;
while (currentIndex != 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
function arrayRegex(elementsNeededToPass, array, regex) {
var elementsPassed = 0;
for (currElement = 0; currElement < array.length && elementsPassed < elementsNeededToPass; currElement++) {
if (regex.test(array[currElement])) {
elementsPassed++;
}
}
return elementsPassed >= elementsNeededToPass;
}
//Export the functions to the other modules
module.exports = { mathRandomInt, shuffleAllNumsTo, arrayRegex };