-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgenerator.js
31 lines (25 loc) · 984 Bytes
/
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
class Generator {
constructor({ startsWith, factor, divideBy, multiple } = {}) {
this.starts_with = startsWith;
this.factor = factor;
this.divide_by = divideBy;
this.multiple = multiple;
this.value = this.nextValue(this.starts_with);
}
nextValue(value = this.value) {
let calculated_value = (value * this.factor) % this.divide_by;
if (typeof this.multiple === 'number') {
// Part two, loop through our "next values" until it is a multiple of our configured `multiple` option
while (calculated_value % this.multiple !== 0) {
calculated_value = (calculated_value * this.factor) % this.divide_by;
}
}
// For part one, just return what we previously calculated
return calculated_value;
}
setNextValue(value = this.value) {
this.value = this.nextValue(value);
return this.value;
}
}
module.exports = Generator;