Skip to content

Instantly share code, notes, and snippets.

@jakekara
Created August 23, 2016 18:58
Show Gist options
  • Save jakekara/1bc4eccba5f3e7b69ad413294de724ea to your computer and use it in GitHub Desktop.
Save jakekara/1bc4eccba5f3e7b69ad413294de724ea to your computer and use it in GitHub Desktop.
// Ratchet.js
// ----------
//
// Pass numbers to Ratchet object using the .add(val) method
// and retrieve the min and max value with .min() and .max()
// Or write custom methods using the .f method used to
// implement the .min() and .max() methods.
// Constructor
// ex: var r = new Ratchet();
Ratchet = function (){
this.__vals = [];
}
// Get the values that have been passed to the ratchet
// ex: r.vals()
Ratchet.prototype.vals = function (){
return this.__vals;
}
// Apply a function to the values array
Ratchet.prototype.f = function(f) {
return f.apply(null, this.__vals);
}
// Get the max value in the values array
Ratchet.prototype.max = function() {
return this.f(Math.max);
}
// Get the min value
Ratchet.prototype.min = function() {
return this.f(Math.min);
}
// Add a new value
// ex: r.add(10);
// r.add(20);
// r.max(); // returns 20
Ratchet.prototype.add = function (val) {
this.__vals.push(val);
return this
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment