proj euler 13

by David Marshall

JavaScript

function BigNum() {
  this.bigN = []
  for (let i = 0; i < 30; i++) {
    this.bigN.push(null)
  }
}

BigNum.prototype.checkNum = function(tier) {
  let t
  while (tier < this.bigN.length) {
    if (this.bigN[tier] > 999) {
      t = this.bigN[tier] % 1000;
      if (tier + 1 == this.bigN.length) {
        this.bigN.push([Math.floor(this.bigN[tier] / 1000)]);
      } else {
        if (!this.bigN[tier + 1]) {
          this.bigN[tier + 1] = Math.floor(this.bigN[tier] / 1000);
        } else {
          this.bigN[tier + 1] += Math.floor(this.bigN[tier] / 1000);
        }
      }
      this.bigN[tier] = t;
      console.log(this.bigN)
    } else {
      return;
    }
    tier += 1;
  }
}

BigNum.prototype.add = function(amt) {
  let t = 0
  if (amt % 3 != 0) {
    t = parseInt(amt.slice(0, amt % 3))
  }
  let temp = amt.slice(amt % 3).match(/.{1,3}/g).map(num => parseInt(num))
  temp.splice(0, 0, t)
  temp.forEach((num, idx) => {
    if (this.bigN[idx] == null) {
      this.bigN[idx] = num
    } else {
      this.bigN[idx] += num
    }
  })
  //console.log(temp)
  //this.bigN[0] += amt;
  this.checkNum(0);
}
BigNum.prototype.getNum = function() {
  console.log(this.bigN)
  let r = this.bigN[0].toString();
  for (let i = 1; i < this.bigN.length; i++) {
    r += this.bigN[i] ? this.bigN[i].toString() : '';
  }
  return r;
}

let num = new BigNum();
let numbers=[]
num.add('37107287533902102798797998220837590246510135740250')
num.add

console.log(num.getNum())