-
Notifications
You must be signed in to change notification settings - Fork 0
/
numberOfCarry.js
40 lines (38 loc) · 907 Bytes
/
numberOfCarry.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
//#region toptal interview
numberOfCarryOperation("533395", "29")
function numberOfCarryOperation(x, y){
var carry;
var iteration;
var xIteration = x.length - 1;
var yIteration = y.length - 1;
if(x.length > y.length) {
iteration = x.length;
} else{
iteration = y.length;
}
var count = 0;
var carry = 0;
while(iteration > 0 ) {
var _x;
var _y;
if(xIteration >= 0){
_x = x[xIteration];
xIteration--;
}
if(yIteration >= 0){
_y = y[yIteration];
yIteration--;
}
var result = +_x + +_y + carry;
if(result > 9){
console.log("1 is carried");
carry = 1;
count++;
}else{
carry = 0;
}
iteration--;
}
console.log(+x + +y, count + " carry operation");
}
//#endregion