exportconstadd=(a:string,b:string):string=>{constre=/^\d+$/if(!re.test(a)||!re.test(b)){thrownewError(`{a} and {b} must be positive integers`)}constresult=[]a=a.split('').reverse().join('')b=b.split('').reverse().join('')letcarry=0for(leti=0;i<Math.max(a.length,b.length);i++){consta_=a.length>i?parseInt(a[i]):0constb_=b.length>i?parseInt(b[i]):0letsum=a_+b_+carrycarry=Math.floor(sum/10)if(carry!==0){sum=sum%10}result.push(String(sum))}if(carry>0){result.push(carry)}returnresult.reverse().join('')}
Test
import{add}from'.'it('should throw an error if numbers are not all digits',()=>{expect(()=>add('','')).toThrow()expect(()=>add('abc','321')).toThrow()expect(()=>add('11','xyz')).toThrow()})it('should return sum of two numbers',()=>{expect(add('0','0')).toBe('0')expect(add('123','321')).toBe('444')expect(add('11','99')).toBe('110')expect(add('1','199')).toBe('200')})it('should return sum of two really big numbers',()=>{expect(add('123456789','987654321')).toBe('1111111110')expect(add('12345678910123456789','987654323109876543210')).toBe('1000000002019999999999',)})
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.
fnadd(num1:&str,num2:&str)->Result<String,String>{constZERO_CHAR:u8='0'asu8;letmutreversed_result=Vec::<u8>::new();letmax_digits=num1.len().max(num2.len());macro_rules!convert_num{($num:expr)=>{$num.chars().rev().chain(['0'].iter().cloned().cycle())}}letdigits1=convert_num!(num1);letdigits2=convert_num!(num2);letmutcarry=0;for(digit1,digit2)indigits1.zip(digits2).take(max_digits){fndigit_to_numeric(digit:char)->Result<u8,String>{if'0'<=digit&&digit<='9'{Ok(digitasu8-ZERO_CHAR)}else{Err(format!("{:?} is not a digit",digit))}}letdigit1=digit_to_numeric(digit1)?;letdigit2=digit_to_numeric(digit2)?;letdigit_sum=digit1+digit2+carry;carry=digit_sum/10;assert!(carry<10);reversed_result.push(digit_sum%10+ZERO_CHAR);}if0<carry{reversed_result.push(carry+ZERO_CHAR);}reversed_result.reverse();Ok(String::from_utf8(reversed_result).expect("We only added digits"))}
I'm a Software Engineer and a teacher.
There's no feeling quite like the one you get when you watch someone's eyes light up learning something they didn't know.
Typescript includes tests
github.com/kiliman/dev-to-daily-ch...
Test
A simple oneliner in JS:
Can't you just do that with jQuery?
There's a
BigInttype 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.
Java
Python for arbitrarily large numbers represented as strings:
Here is the simple solution with JavaScript:
Rust:
Javascript
Swift function :
And with a closure :
Isn't
Inta 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)Ruby:
JS
python
A more cool JS solution: