Javascript Number times(action)

Description

Javascript Number times(action)


"use strict";//ww w .j  av  a 2s .c o m

Number.prototype.times = function(action) {
  var i;
  for (i = 1; i <= this; i++) {
    action();
  }
};

(5).times(function () { console.log("OMG!"); });

Javascript Number times(action)

"use strict";//from  w  ww .j a v a  2s  . c o m

Number.prototype.times = function(action){
  var i = 0;
  while(i < this){
    action();
    i++;
  }
};

Javascript Number times(action)

"use strict";//  w w w. j  a v a  2 s  .  co m


Number.prototype.times = function(action){
    if(this < 0 || this%1 !== 0){
        throw new TypeError("Wrong input!");
    }
    var i = 0;
    while(i < this){
       action();
        i += 1;
    }
};

(5).times(function() {
    console.log("OMG!");
});

exports.Number.prototype.times = Number.prototype.times;

Javascript Number times(action)

Number.prototype.times = function(action) {
  var arr = [].range(1, this);
  arr.forEach(function() { action(); });
};

console.log((5).times(function () { console.log("Hey!!!"); }));

Javascript Number times(action)

Number.prototype.times = function(action) {
  var counter = this
  while (counter-- > 0)
    action()/*from   w ww.j av  a  2s  . co m*/
}

Javascript Number times(action)

Number.prototype.times = function(action) {
  return [].range(1, this).forEach(action);
};



PreviousNext

Related