A class to help with specific use-cases where you are using a percentage (ex. 1 / n) but don't know n until runtime.
1 / n
n
const autoPercentage = new AutoPercentage();const steps = [ { name: 'init', progress: autoPercentage.percentage() }, { name: 'modify', progress: autoPercentage.percentage() }, { name: 'verify', progress: autoPercentage.percentage() }, { name: 'cleanup', progress: 0 }];let progress = 0;for (const step of steps) { if (progress === 1) { return; } progress += step.progress; console.log(`${step.name} finished - job is ${Math.round(progress * 100)}% complete`);} Copy
const autoPercentage = new AutoPercentage();const steps = [ { name: 'init', progress: autoPercentage.percentage() }, { name: 'modify', progress: autoPercentage.percentage() }, { name: 'verify', progress: autoPercentage.percentage() }, { name: 'cleanup', progress: 0 }];let progress = 0;for (const step of steps) { if (progress === 1) { return; } progress += step.progress; console.log(`${step.name} finished - job is ${Math.round(progress * 100)}% complete`);}
Increment and get the number of times the percentage has been incremented.
Time complexity: O(1)
Space complexity: O(1)
The number of times this percentage has been incremented
const autoPercentage = new AutoPercentage();const a = autoPercentage.count();Number(a); // 1const b = autoPercentage.count();Number(a); // 2Number(b); // 2 Copy
const autoPercentage = new AutoPercentage();const a = autoPercentage.count();Number(a); // 1const b = autoPercentage.count();Number(a); // 2Number(b); // 2
Increment and get the percentage.
The percentage
const autoPercentage = new AutoPercentage();const a = autoPercentage.percentage();Number(a); // 1const b = autoPercentage.percentage();Number(a); // 0.5Number(b); // 0.5 Copy
const autoPercentage = new AutoPercentage();const a = autoPercentage.percentage();Number(a); // 1const b = autoPercentage.percentage();Number(a); // 0.5Number(b); // 0.5
A class to help with specific use-cases where you are using a percentage (ex.
1 / n
) but don't known
until runtime.Example