Oke Tega
Oke Tega

Reputation: 883

Sum Strings as Numbers

I am trying to solve a kata that seems to be simple on codewars but i seem to not be getting it right.

The instruction for this is as simple as below

Given the string representations of two integers, return the string representation of the sum of those integers.

For example:

sumStrings('1','2') // => '3'

A string representation of an integer will contain no characters besides the ten numerals "0" to "9".

And this is what i have tried

function sumStrings(a,b) { 
  return ((+a) + (+b)).toString();
}

But the results solves all except two and these are the errors i get

sumStrings('712569312664357328695151392', '8100824045303269669937') - Expected: '712577413488402631964821329', instead got: '7.125774134884027e+26'

sumStrings('50095301248058391139327916261', '81055900096023504197206408605') - Expected: '131151201344081895336534324866', instead got: '1.3115120134408189e+29'

I don't seem to understand where the issues is from. Any help would help thanks.

Upvotes: 3

Views: 9731

Answers (5)

Jam
Jam

Reputation: 83

Converting from a string to a number or vice versa is not perfect in any language, they will be off by some digits. This doesn't seem to affect small numbers, but it affects big numbers a lot. The function could go like this.

function sumStrings(a, b) {
  return (BigInt(a) + BigInt(b)).toString()   // or parseInt for both
}

However, it's still not perfect since if we try to do:

console.log((4213213124214211215421314213.0 + 124214321214213434213124211.0) === sumStrings('4213213124214211215421314213', '124214321214213434213124211'))

The output would be false.

Upvotes: 0

Xayrulloh Abduvohidov
Xayrulloh Abduvohidov

Reputation: 31

My solution is:

function sumStrings(a,b) { 
   return BigInt(a) + BigInt(b) + ''
}

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386560

You could pop the digits and collect with a carry over for the next digit.

function add(a, b) {
    var aa = Array.from(a, Number),
        bb = Array.from(b, Number),
        result = [],
        carry = 0,
        i = Math.max(a.length, b.length);
        
    while (i--) {
        carry += (aa.pop() || 0) + (bb.pop() || 0);
        result.unshift(carry % 10);
        carry = Math.floor(carry / 10);
    }
    while (carry) {
        result.unshift(carry % 10);
        carry = Math.floor(carry / 10);
    }
    return result.join('');
}

console.log(add('712569312664357328695151392', '8100824045303269669937'));
console.log(add('50095301248058391139327916261', '81055900096023504197206408605'));

Upvotes: 3

Julio Oliveira
Julio Oliveira

Reputation: 121

The value you entered is bigger than the int type max value. You can try changing your code to:

 function sumStrings(a,b) { 
  return ((BigInt(a)) + BigInt(b)).toString();
}

This way it should return the right value

Upvotes: 12

Shobi
Shobi

Reputation: 11461

The problem is that regular javascript integers are not having enough space to store that much big number, So it uses the exponential notation to not lose its precision

what you can do is split each number into parts and add them separately,

one such example is here SO answer

Upvotes: 0

Related Questions