3

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.

Oke Tega
  • 850
  • 10
  • 21
  • Similar to this question? https://stackoverflow.com/questions/1685680/how-to-avoid-scientific-notation-for-large-numbers-in-javascript – Peter M. Nov 09 '18 at 14:32
  • 1
    In this exercise you're expected to simulate a pen and paper addition, digit by digit. The simplest and most widely taught method is the column addition from the right. – georg Nov 09 '18 at 15:49

5 Answers5

12

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

  • This is the correct answer but unfortunately the Kata he speaks of doesn't allow for BigInt to be used for some dumb reason. https://www.codewars.com/kata/5324945e2ece5e1f32000370/train/javascript – Gustavo Maximo Aug 18 '20 at 23:15
  • When given problems for the sake of problems, sometimes you need to come up with clever solutions, such as copy/pasting a minified version of a BigInt polyfill – Samathingamajig Jan 01 '23 at 09:32
3

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'));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

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

Shobi
  • 10,374
  • 6
  • 46
  • 82
0

My solution is:

function sumStrings(a,b) { 
   return BigInt(a) + BigInt(b) + ''
}
  • 1
    Welcome to Stack Overflow! Your answer is not significantly different from the [highest upvoted answer](https://stackoverflow.com/a/53227889/12101554) (the only difference is how you convert it to a string). To avoid having duplicate answers, either upvote their answer or improve your answer to have significantly more information/explanation (which isn't really possible in this case) or a different approach – Samathingamajig Jan 01 '23 at 09:35
  • you right. But mines is short. Joke – Xayrulloh Abduvohidov Jan 13 '23 at 06:10
0

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.

DSDmark
  • 1,045
  • 5
  • 11
  • 25
Jam
  • 65
  • 7
  • 2
    Welcome to Stack Overflow! Your answer is not significantly different from the [highest upvoted answer](https://stackoverflow.com/a/53227889/12101554) (in fact, the comment to use `parseInt` causes the same bug as the original). To avoid having duplicate answers, either upvote their answer or improve your answer to have significantly more information/explanation (which isn't really possible in this case) or a different approach – Samathingamajig Jan 01 '23 at 09:35
  • i'm new into here kinda so sure, i'll learn from this place – Jam Jan 01 '23 at 10:07