Nodejs Number Calculate testNumber()

Here you can find the source of testNumber()

Method Source Code

// What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

function Number(){
  this.start = 20/*from w ww.  j  a v a2  s  .  c o  m*/
  this.testNumber();
  this.answer = 1
};

Number.prototype.testNumber = function(){
  var i = 1;
  var start = this.start;
  while(i <= 20){
    if(this.start % i != 0){
      // console.log(i)
      return false
      }
    else{
      i ++;
    }
  }
}



Number.prototype.getAnswer = function(){
  var self = this
  if(this.testNumber() !== false){  
  this.answer = this.start
  console.log(this.answer);
  }
  else{
  this.start += 20
  setImmediate(function(){self.getAnswer()})  
  }

}



var n = new Number();

n.getAnswer();

Related

  1. square()
    Number.prototype.square = function() {
      return this * this;
    };
    
  2. step(to, add_remove_step)
    Number.prototype.step = function (to, add_remove_step) {
      var from = this.valueOf();
      var ch = add_remove_step.match(/[\+,\-]/g)[0];
      var i = from;
      var arr = [];
      if (ch === '+') {
        while (i <= to) {
          arr.push(i);
          i = i + parseInt(add_remove_step);
    ...
    
  3. subNum(n)
    Number.prototype.subNum = function(n){
      var f_x = parseFloat(this);
      if (isNaN(f_x)){
        return false;
      f_x = Math.floor(f_x*Math.pow(10,n)+0.0001)/Math.pow(10,n);
      var s_x = f_x.toString();
      var pos_decimal = s_x.indexOf('.');
      if (pos_decimal < 0){
    ...
    
  4. succ()
    Number.prototype.succ = function() {
        return this + 1;
    };
    
  5. tan()
    Number.prototype.tan = function() {
        return Math.tan(this);
    
  6. trim(a, b)
    Number.prototype.trim = function(a, b) {
      var min = Math.min(a, b),
          max = Math.max(a, b);
      return Math.min(Math.max(parseInt(this, 10), min), max);
    };
    
  7. tween(target, position)
    Number.prototype.tween = function(target, position){
      return this + (target-this) * position;
    };
    
  8. twos(n)
    Number.prototype.twos = function(n) {
      if (this >= 0) {return pad(this.toString(2),n)}
      var max = Math.pow(2,n-1);
      return "1" + pad((max + this).toString(2),n-1)
    function pad(num, size) {
        var s = "00000000000000000000" + num;
        return s.substr(s.length-size);
    Number.prototype.twos = function(n) {
      var ret = "";
      while(n)
        ret += ( (this >> --n) & (1) );
      return ret;
    };
    
  9. valueOf() return x; };
    function foo(a, b) {
        var result = a + b;
        if (result)
            return (a + b) + result + this;
        else
            return this.f;
    noInline(foo);
    var x;
    ...