1 function getNextInt(int) {
2 const config = int.reverse().reduce(
3 (config, digit) => {
4 let sum = digit + config.increment,
5 nextInt = config.nextInt,
6 increment = 0;
7
8 if (sum > 9) {
9 nextInt.push(sum - 10);
10 increment = 1;
11 } else {
12 nextInt.push(sum);
13 }
14
15 return {
16 increment,
17 nextInt,
18 };
19 },
20 { increment: 1, nextInt: [] }
21 );
22
23 if (config.increment) {
24 config.nextInt.push(1);
25 }
26
27 return config.nextInt.reverse();
28 }
29
30 console.log(getNextInt([9, 9, 9])); // [1, 0, 0, 0]
31 console.log(getNextInt([9, 9, 9, 0])); // [9, 9, 9, 1]
32 console.log(getNextInt([9, 9, 9, 0, 1])); // [9, 9, 9, 0, 2]
33 console.log(getNextInt([9, 9, 9, 0, 9])); // [9, 9, 9, 1, 0]