Let's say I have the following 2 Arrays:
let a = [5,5,5,5,0]
let b = [5,5,5,5,0]
Assuming that both arrays has the same length:
I want to get a value from an element of one Array and add the +1 itself and to other remaining elements. If the the picked value is still bigger than zero, then it should also continue adding it to elements of other array from starting index.
Example_1: If I pick a[2]
then the outcome should be:
a = [5,5,1,6,1] // a[2] became 0 after picking 5, then in each remaining element we add +1 and continue to next array.
b = [6,6,5,5,0]
Example_2: Or If I pick b[3]
from initial arrays, then the outcome should be:
a = [6,6,6,5,0] // b[3] became 0 after picking 5, then in each remaining element we add +1 and move to next array until our value becomes zero
b = [5,5,5,1,1]
Here is my attempt:
let pickedValue = a[2]
for (let index = 2; index < a.length; index++) {
if (pickedValue > 0) {
a[index] += 1
pickedValue--
}
}
for (let index = 0; index < pickedValue; index++) {
if (pickedValue > 0) {
b[index] += 1
pickedValue--
}
}
Can we do this in more dynamic way with maybe one loop?