-4

For non-negative integers num1 and num2, represented as strings, return the sum of num1 and num2.

My solution was to create a filter that has the indexOf with a value of the numbers.

const addStrings = function(num1, num2) {
    let filter = "0123456789";
    
    let x = 0;
    for(let i=0; i<num1.length; i++) {
        x = x + (filter.indexOf(num1[i]) * Math.pow(10, num1.length-1-i));
    }
    
    let y = 0;
    for(i=0; i<num2.length; i++) {
        y = y + (filter.indexOf(num2[i]) * Math.pow(10, num2.length-1-i));
    }
    
    return (x+y).toString();
};

It work in most of the cases. However, if the inputs are:

"9333852702227987"
"85731737104263"

It will return the wrong sum: "9419584439332252". I can't understand why it is converting the numbers wrongly.

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
myTest532 myTest532
  • 2,091
  • 3
  • 35
  • 78
  • 5
    Does this answer your question? [What is JavaScript's highest integer value that a number can go to without losing precision?](https://stackoverflow.com/questions/307179/what-is-javascripts-highest-integer-value-that-a-number-can-go-to-without-losin) – ASDFGerte Jul 16 '20 at 15:27
  • [JavaScript summing large integers](https://stackoverflow.com/questions/4557509/javascript-summing-large-integers). I believe the point of the assignment/test is to check that you understand the precision of large integers. – Guy Incognito Jul 16 '20 at 15:28
  • I'd got through the strings from right to left, compare both "sub-strings", figure out the sum with carry as singles digit and a carry, and add the digit to the result on it's left side, repeat, perhaps padding with "0"s to simplify, until done. – iAmOren Jul 16 '20 at 15:33
  • I've coded an answer - thanks for the challenge! – iAmOren Jul 16 '20 at 16:15

3 Answers3

1

I've created an object whose keys are digit1, digit2, and carry - all as strings.
The function takes two stringed numbers and adds them "digit by digit" (actually, character by character, as keys to object + carry).
The limit on the "numbers" is the limit on the length of string (minus 1 for the carry).
This could even be adapted to deal with decimals (I leave that to you).

Here's my code:

var sumObject={};
for(var i=0; i<=9; i++) {
  for(var j=0; j<=9; j++) {
    var sum, carry, digit, obj;
    sum=i+j;
    carry=sum>9?"1":"0";
    digit=sum%10;
    obj={sum:""+digit, carry:carry};
    sumObject[""+i+j+"0"]=obj;
    sum=i+j+1;
    carry=sum>9?"1":"0";
    digit=sum%10;
    obj={sum:""+digit, carry:carry};
    sumObject[""+i+j+"1"]=obj;
  }
}

function sum2StringedNumbers(sn1, sn2) {
  var answer="";
  var maxLength=Math.max(sn1.length, sn2.length);
  sn1=("0".repeat(maxLength)+sn1).slice(-maxLength);
  sn2=("0".repeat(maxLength)+sn2).slice(-maxLength);
  var carry="0";
  for(var i=maxLength; i>0; i--) {
    var key=""+sn1.charAt(i-1)+sn2.charAt(i-1)+carry;
    var answer=sumObject[key].sum+answer;
    carry=sumObject[key].carry;
  }
  if(carry=="1") answer="1"+answer;
  return answer;
}
danronmoon
  • 3,814
  • 5
  • 34
  • 56
iAmOren
  • 2,760
  • 2
  • 11
  • 23
0

The sum of those numbers is larger than Number.MAX_SAFE_INTEGER. You need to use BigInts instead.

const addStrings = function(num1, num2) {
    let filter = "0123456789";
    
    let x = 0n;
    for(let i=0; i<num1.length; i++) {
        x = x + (BigInt(filter.indexOf(num1[i])) * 10n ** BigInt(num1.length-1-i));
    }
    
    let y = 0n;
    for(i=0; i<num2.length; i++) {
        y = y + (BigInt(filter.indexOf(num2[i])) * 10n ** BigInt(num2.length-1-i));
    }
    
    return (x+y).toString();
};
console.log(addStrings("9333852702227987","85731737104263"));
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
-1

You can skip a lot of that code and convert your string directly to a BigInt and calculate from there. Then use toString() to bring it back to a string. Check it out:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt

(BigInt("9333852702227987") + BigInt("85731737104263")).toString();
// 9419584439332250
SmujMaiku
  • 603
  • 6
  • 10