forked from udjamaflip/combination-generator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerator.js
executable file
·56 lines (43 loc) · 1.57 KB
/
generator.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
44
45
46
47
48
49
50
51
52
53
54
55
56
#! /usr/bin/env node
'use strict'
function loadCombinationList(charList, minCharCount, maxCharCount) {
let comboList = [],
charPos = [],
currentCodeLength = minCharCount, //get the first code to work with as a starting point.
currentCode = '',
lastCharacterIndex = charList.length;
//generate base charPos array
for (let i = 0; i < maxCharCount; i++) {
charPos.push(0);
}
while (currentCodeLength <= maxCharCount) {
if (currentCode.indexOf('undefined') > -1) {
break;
}
//build the code and increment the specific charpos array index
currentCode = '';
for (let i = 0; i < currentCodeLength; i++) {
currentCode += charList[charPos[i]];
}
charPos[currentCodeLength-1]++;
//save the code into the array
comboList.push(currentCode);
//now handle overflow - gotta go through array backwards
let overflowCount = 0;
for (let i = (charPos.length-1); i >= 0; i--) {
if (charPos[i] >= lastCharacterIndex) {
overflowCount++;
charPos[i] = 0;
//if our current index position isn't 0
//increase the previous character
if (i != 0) { charPos[i-1]++; }
}
}
//do we need to increase character count?
if (overflowCount === currentCodeLength) {
currentCodeLength++;
}
}
return comboList;
}
module.exports = loadCombinationList;