-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumStrings.js
More file actions
35 lines (24 loc) · 792 Bytes
/
sumStrings.js
File metadata and controls
35 lines (24 loc) · 792 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
Source: Codewars
Difficulty: 4 kyu
Title: Sum Strings as Numbers
Description:
Given the string representations of two integers,
return the string representation of the sum of those integers.
A string representation of an integer will contain
no characters besides the ten numerals "0" to "9".
Example:
sumStrings('712569312664357328695151392', '8100824045303269669937') => '712577413488402631964821329'
*/
function sumStrings(a,b) {
var a = a.split('');
var b = b.split('');
var solution = [];
var remainder = false;
while (a.length || b.length || remainder) {
sum = remainder + ~~a.pop() + ~~b.pop();
solution.push( sum % 10 );
remainder = sum > 9;
}
return solution.reverse().join('').replace(/^0/, '');
}