简体   繁体   中英

Javascript add two numbers that are string without using parse

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.

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;
}

The sum of those numbers is larger than Number.MAX_SAFE_INTEGER . You need to use BigInt s 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"));

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM