-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSum_Strings.js
More file actions
41 lines (29 loc) · 1.01 KB
/
Sum_Strings.js
File metadata and controls
41 lines (29 loc) · 1.01 KB
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
36
37
38
39
40
41
/* ----------------------------------------------------------------------------------------
Create a function that takes 2 integers in form of a string as an input, and outputs
the sum (also as a string):
Example: (Input1, Input2 -->Output)
"4", "5" --> "9"
"34", "5" --> "39"
"", "" --> "0"
"2", "" --> "2"
"-5", "3" --> "-2"
Notes:
If either input is an empty string, consider it as zero.
Inputs and the expected output will never exceed the signed 32-bit integer limit
(2^31 - 1)
---------------------------------------------------------------------------------------- */
/*
parseInt(): parses a string argument and returns an integer of the specified radix
Number.prototype.toString(): returns a string representing this number value
*/
function sumStr(a, b) {
return (
(parseInt(a) ? parseInt(a) : 0) + (parseInt(b) ? parseInt(b) : 0)
).toString();
}
/*
Alternative solution
*/
function sumStr(a, b) {
return String(Number(a) + Number(b));
}