-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge-5.js
62 lines (40 loc) · 1.39 KB
/
challenge-5.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Two integer numbers are added using the column addition method. When using this method, some additions of digits produce non-zero carries to the next positions. Your task is to calculate the number of non-zero carries that will occur while adding the given numbers.
// The numbers are added in base 10.
// Example
// For a = 543 and b = 3456, the output should be 0
// For a = 1927 and b = 6426, the output should be 2
// For a = 9999 and b = 1, the output should be 4
// Input/Output
// [input] integer a
// A positive integer.
// Constraints: 1 ≤ a ≤ 10^7
// [input] integer b
// A positive integer.
// Constraints: 1 ≤ b ≤ 10^7
// [output] an integer
// #solution
function numberOfCarries(a, b) {
//coding and coding..
const lenA = `${a}`.length;
const lenB = `${b}`.length;
let len = lenA>lenB?lenA:lenB;
let i = 0;
let carried = 0;
let totalCarried = 0;
while(i<len){
const digitA = a%10;
const digitB = b%10;
if(digitA+digitB+carried>9){
carried=1;
totalCarried++;
}else{
carried=0;
}
a=Math.floor(a/10);
b=Math.floor(b/10);
i++;
}
return totalCarried;
}
numberOfCarries(9999,1); //4
numberOfCarries(543,3456); //0