0
class BigNumber{

  constructor(n){
    this.number = this.digitSplitter(n)
  }

  //adds an integer to the big number
  add(x){
    x = this.digitSplitter(x)
    var j = x.length-1, temp = 0;
    var length = x.length >= this.number.length ? x.length : this.number.length
    for(var i = length-1; i >= 0;i--,j--){
      if(x.length > this.number.length)
        temp += x[j]
      else if(x.length == this.number.length)
        temp += x[j] + this.number[i]
      else
        temp += this.number[i]
  
      if(temp >= 10){
        this.number[i] = temp%10
        temp -= temp%10
        temp /= 10
      }else {
        this.number[i] = temp
        temp = 0
      }
    }
    return this.number
  }

  //digit splitter but for Integers larger than 15
  digitSplitter(n){
    var output;
    print("number: "+n)
    print("type?: "+ typeof n)
    output = n.split('').map(Number)
    print(output)
    return output;
  }
  //simply counts digits in a number
  digitCount(n){
    var count=0
    while(n>1){
      n/=10;
      count++
    }
    return count
  }
}

WHERE THE BAD CODE IS:

constructor(n){
  this.number = this.digitSplitter(n)
}

WORKING CODE:

 constructor(n){
   n = this.digitSplitter(n)
 }

NOT WORKING CODE

 constructor(n){
   n = this.digitSplitter(n)
   this.number = n
 }

THE OUTPUT: (suppose to count down from 9 to 1, 4 times) enter image description here

THE INPUT:

enter image description here

any help would be great, I'm thinking it has something todo with pointers and addresses if js even has control on that. I'm not sure what a good work around would be.

Jacob Burgess
  • 55
  • 1
  • 7
  • what makes it "non working"? – evolutionxbox Jul 03 '21 at 00:47
  • @evolutionxbox it scrambles my array when i assign the value to a member variable. but my array stays in order when i assign the value to a local variable...same code , just different storage areas – Jacob Burgess Jul 03 '21 at 00:48
  • Aside from a missing leading 1 digit (due to a bug in your `add` logic that makes it stop too early), that looks like the right output for 987654321987654321987654321987654321+987654321987654321987654321987654321 to me. Did you expect the result to be 987654321987654321987654321987654321 somehow? – user2357112 Jul 03 '21 at 00:55
  • @user2357112supportsMonica okay so I'm not worried about what goes on in the add function and weather or not it works property. the first line of output is from right before the .split().map expression that turns said string into a an integer array. after it convert it to an integer array its all messed up. but then my program call on it again in the first line of the add function and it works properly... i can get the first string to parse correctly but only when it assigned to a local variable rather than a member variable – Jacob Burgess Jul 03 '21 at 01:14
  • That's console weirdness - it's showing you the wrong state of the right array. It's showing you the post-`add` state even though you logged it pre-`add`. It's a good idea to stringify objects (e.g. with `JSON.stringify`) before logging them to the console to avoid this kind of problem. – user2357112 Jul 03 '21 at 01:17
  • @user2357112supportsMonica Oh, wow okay, your completely right, totally solved my problem, thank you kindly. it would have taken me some ridiculous sloppy work around. thanks again – Jacob Burgess Jul 03 '21 at 01:26
  • Related: [Is Chrome's JavaScript console lazy about evaluating arrays?](/q/4057440/4642212). – Sebastian Simon Jul 03 '21 at 04:57
  • @SebastianSimon its a p5.js console – Jacob Burgess Jul 03 '21 at 05:59

1 Answers1

0

That's console weirdness - it's showing you the wrong state of the right array. It's showing you the post-add state even though you logged it pre-add. It's a good idea to stringify objects (e.g. with JSON.stringify) before logging them to the console to avoid this kind of problem. – user2357112 supports Monica

i cant mark it, but said user solved my issue

Jacob Burgess
  • 55
  • 1
  • 7