Skip to content

Instantly share code, notes, and snippets.

@batazo
Created June 22, 2022 21:38
Show Gist options
  • Select an option

  • Save batazo/a828d434661fa20ed6586426b522ebd3 to your computer and use it in GitHub Desktop.

Select an option

Save batazo/a828d434661fa20ed6586426b522ebd3 to your computer and use it in GitHub Desktop.
COUNTER INSTANCE
console.clear();
const counter = new Counter({min:-15, from: 0, step: 2, max: 100 });
const addStep = ()=>counter.incr();
const subStep = ()=>counter.decr();
const resetCounter = ()=>counter.reset()
const setStep = (number)=>counter.setStep(number)
function Counter({min, from, step, max}){
let actual = from;
const defaults = {min, from, step, max, actual}
this.incr = function(){
return actual >=max? max : actual += step
}
this.decr = function(){
return actual <=min? min : actual -= step
}
this.reset = function(){
return {min, from, step, max, actual} = defaults;
}
this.setStep = function(number){
if(typeof number === 'number'){
return step = number;
} else {
return "Not a number"
}
}
this.getActual = function(){return actual}
this.getDefaults = function(){return defaults}
}
// Tests
console.log('ADD STEPS with 2:', addStep(), addStep(), addStep(), addStep(), addStep())
setStep(10)
console.log('ADD STEPS with 10:', addStep(), addStep(), addStep(), addStep(), addStep())
console.log('ACTUAL:', counter.getActual())
console.log('SUBSTRACTION with 10:', subStep(), subStep(),subStep())
setStep(15)
console.log('SUBSTRACTION with 15:', subStep(), subStep(),subStep(),subStep(),subStep())
console.log(counter.getDefaults())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment