Nodejs Array Count count(n)

Here you can find the source of count(n)

Method Source Code

Array.prototype.count = function(n) {
  var count = 0;/*from w ww.j  a  v a  2 s. c o  m*/
  for (var i = 0; i < this.length; i++) {
    if (this[i] == n) count++;
  }
  return count;
}

function primeFactors(n) {
  var f = [];
  var d = 2;
  while (n > 1) {
    while (n%d==0) {
      f.push(d);
      n = n/d;
    }
    d++;
  }
  return f;
}

function smallestDivTo(x) {
  var factors = [];
  for (var i = 2; i <= x; i++) {
    var f = primeFactors(i);
    for (var j = 0; j < f.length; j++) {
      if (!factors[f[j]] || (f.count(f[j]) > factors[f[j]])) factors[f[j]] = f.count(f[j]);
    }
  }
  var result = 1;
  for (var i = 0; i < factors.length; i++) {
    if (factors[i]) result *= Math.pow(i, factors[i]);
  }
  return result;
}

test("smallestDivTo", function() {
  equal(smallestDivTo(10), 2520);
  equal(smallestDivTo(20), 232792560);
});

Related

  1. count()
    Array.prototype.count = function(){
        return this.length;
    };
    
  2. count()
    Array.prototype.count = function () {
        return this.length;
    let numbers = [1,2,3,4];
    let moreNumbers = new Array(1,2,3,4);
    console.log(numbers.count());
    console.log(moreNumbers.count());
    
  3. count()
    Array.prototype.count = function () {
        var temp = {};
        for (var i = 0; i < this.length; i++) {
            if (!(temp[this[i]])) {
                temp[this[i]] = 1;
            } else {
                temp[this[i]]++;
        return temp;
    
  4. count()
    function letterCapitalize(string) {
        var stringArray = string.split(" ");
        var sentence = stringArray.map(function(word) {
            var firstLetter = word[0];
            var otherLetters = word.slice(1);
            var capFirst = firstLetter.toUpperCase();
            return capFirst + otherLetters;
        })
        return sentence.join(" ");
    ...
    
  5. count(filter)
    Array.prototype.count = function(filter)
      var ct = 0;
      for (var i = 0; i< this.length; i++) {
        if(filter(this[i])){
          ct++;
      return ct;
    ...
    
  6. count(value)
    Array.prototype.count = function (value) {
      var count = 0, i;
      for (i = 0; i < this.length; ++i) {
        if (this[i] === value) {
          ++count;
      return count;
    };
    ...
    
  7. count(value)
    Array.prototype.count = function (value) {
      var count = 0, i;
      for (i = 0; i < this.length; ++i) {
        if (this[i] === value) {
          ++count;
      return count;
    };
    ...
    
  8. countCattle( kind )
    Array.prototype.countCattle = function ( kind ){
      var numKind = 0;
      for(var i = 0; i<this.length; i++){
        if(this[i].type == kind){
          numKind++;
      return numKind;
    };
    ...
    
  9. countCattle(cattle)
    Array.prototype.countCattle = function(cattle){
      var number = 0;
      for(var i = 0; i < this.length; i ++){
        if(this[i].type == cattle){
          number ++ ;
      return number;
    };
    ...