js-helpers

Times

Times (Number count, Function cb[, thisArg])

Times first count items from array

Arguments

Number count

Any Number

Function cb

Any Function to call count times.

thisArg optional

Will be the value of this within cb.

Use

import { Times } from "@taystack/js-helpers";

let val = 1;
Times(4, (i) => val += i);
// val + i
// 1   + 0 = 1
// 1   + 1 = 2
// 2   + 2 = 4
// 4   + 3 = 7

In a bit of action:

class Fighter {
  constructor() {
    this.attacks = 3;
  }

  get attack() {
    return Random(1, 4);
  }

  attackTarget(target) {
    Times(this.attacks, () => {
      target.takeDamage(this.attack);
    });
  }
}

Source: @taystack/js-helpers hosted on GitHub Author: taystack