We need to sum big numbers and we require your help.
Write a function that returns the sum of two numbers. The input numbers are strings and the function must return a string.
Example:
add("123", "321"); -> "444"
add("11", "99"); -> "110"
Notes:
- The input numbers are big.
- The input is a string of only digits
- The numbers are positives
This challenge comes from Becojo on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (16)
Typescript includes tests
github.com/kiliman/dev-to-daily-ch...
Test
There's a
BigInt
type in vanilla javascript, why not just use that instead?BigInt isn't supported for this one. There was another kata that was multiplying big numbers by a different author and they allowed it, but I guess that's not the route they wanted folks to take.
A simple oneliner in JS:
Can't you just do that with jQuery?
Python for arbitrarily large numbers represented as strings:
Java
Here is the simple solution with JavaScript:
Swift function :
And with a closure :
Isn't
Int
a sized integer? It won't work with numbers that don't fit in 64 bits (or 32 bits, if you compile it for 32 bit architectures)Javascript
Rust:
A more cool JS solution: