Nodejs Function Call times(fn)

Here you can find the source of times(fn)

Method Source Code

'use strict';/*  www  . j  ava  2 s . c  om*/

// Reminiscent of Ruby's Number::times for coffeescript
// Usage:
// 5.times (i) -> console.log i
// 4.times -> t.fd 100; t.rt 90
// http://stackoverflow.com/a/8015103/5191980
Number.prototype.times = function(fn) {
   if (this.valueOf()) {
      for (var i = 0; i < this.valueOf(); i++) {
         fn(i);
      }
   }
};

// Similar to above, but more function
// Usage:
// repeat 5, (i) -> console.log i
// repeat 4, -> t.fd 100; t.rt 90
function repeat(n, fn) {
   for (var i = 0; i < n; i++) {
      fn(i);
   }
}

// Generates a random integer
// Uses p5js's `random` function
function randomInt(a, b) {
   return Math.floor(random(a, b));
}

// Prompts with a message and an optional default value
// If the result cannot be parsed as an integer,
// `null` is returned instead
function promptInt(message, default_) {
   var parsed = parseInt(prompt(message, default_));
   if (isNaN(parsed)) {
      return null;
   }
   return parsed;
}

Related

  1. partial()
    Function.prototype.partial = function(){
        var fn = this, args = Array.prototype.slice.call(arguments);
        return function(){
          var arg = 0;
          for ( var i = 0; i < args.length && arg < arguments.length; i++ )
            if ( args[i] === undefined )
              args[i] = arguments[arg++];
          return fn.apply(this, args);
        };
    ...
    
  2. map(map)
    Function.prototype.map = function (map) {
        var params = this.getParamNames();
        var fn = this;
        for (var i in params)
            fn = fn.bind({}, map[params[i]]);
        return fn;
    };
    
  3. times(f, context)
    Number.prototype.times = function(f, context){
      var n = Number(this);
      for(var i = 0; i < n; i++){
        f.call(context, i);
    };
    
  4. times(f, context)
    function Range(from, to) {
        this.from = from;
        this.to = to;
    };
    Range.prototype = {
        constructor: Range,
        includes: function(x) {
            return this.from <= x && x <= this.to;
        },
    ...
    
  5. times(f, context)
    Number.prototype.times = function(f, context) {
      var n = Number(this);
      for(var i = 0; i < n; i++) f.call(context, i);
    };
    String.prototype.trim = String.prototype.trim || function() {
      if (!this) return this; 
      return this.replace(/^\s+|\s+$/g, ""); 
    };
    Function.prototype.getName = function() {
    ...
    
  6. times(func)
    var i = 0;
    Number.prototype.times = function(func) {
      let n = Math.round(this);  
      if (n <= 0) return null;
      func();
      return (n - 1).times(func);
    console.log(i);
    
  7. times(func)
    Number.prototype.times = function (func) {
      var i;
      for (i = 0; i < this; i += 1) {
        func();
    };
    function random(min, max) {
        var floor = +min,
            ceil = +max;
    ...
    
  8. times(func, scope)
    Number.prototype.times = function(func, scope) {
      var i, v, _results;
      v = this.valueOf();
      i = 0;
      _results = [];
      while (i < v) {
        func.call(scope || window, i);
        _results.push(i++);
      return _results;
    };
    
  9. times(handler)
    Number.prototype.times = function(handler) {
      var index = 0;
      while(index < this) handler(index++);
      return this
    };