Here is the link to a similar question I asked previously. This time I want to change the conditions. Here is the details:
I have an array:
inputArr = [2, 4, 3, 7, 8, 2, 3, 4, 9, 1];
All the numbers are below '10'. I want the cumulative total of the numbers pushed to the output array. But with two conditions:
if the cumulative total is not equal to '10' but closest to '10', after reaching the cumulative total closest to (not exceeding) '10', calculate the difference to '10' and push the difference as another element to the output array right after the last number that makes cumulative total to be closest to '10'.
AND restart pushing the cumulative total to output array from next number in the input array.
So, the output should be:
outputArr = [2, 6, 9, 1, 7, 3, 8, 2, 3, 4, 3, 9, 1];
Step by step explanation of how this output array is created: 
I have tried modifying this code. Here is the modified code:
let input = [2, 4, 3, 7, 8, 2, 3, 4, 9, 1],
output = [],
cumulative = 0;
for (let i = 0; i < input.length; i++) {
cumulative += input[i];
if (cumulative < 10) {
output.push(cumulative)
} else if (cumulative == 10) {
output.push(10)
cumulative = 0
} else {
output.push(10 - output[i - 1])
cumulative = input[i]
output.push(cumulative)
}
}
console.log(output)
This code gives me this output:
outputArr = [2, 6, 9, 1, 7, 9, 8, 10, 3, 7, 0, 9, 10]
Here, only first 4 elements in the output array came as intended. How can I correct this code? Any help is much appreciated.