I have two arrays and I have to increase every single element of the second array till it is equal to the first array. What i got so far (not working correctly):
EDIT: I need every possible iteration of the second array till it is equal. The first array is the limiter so every element of array2 can only be smaller or the same as the element of array1 with the corresponding key e.g. array2[0] can be <= array1[0].
Input:
array1 = [1, 2, 3, 4], array2 = [0, 0, 0, 0]
Desired Output (not necessarily in this order):
[0, 0, 0, 0],[1, 0, 0, 0],[0, 1, 0, 0],[0, 2, 0, 0],[0, 0, 1, 0],[0, 0, 2, 0],[0, 0, 3, 0],[0, 0, 0, 1],[0, 0, 0, 2],[0, 0, 0, 3],[0, 0, 0, 4],[1, 1, 0, 0],[1, 2, 0, 0],[1, 0, 1, 0],[1, 0, 2, 0],[1, 0, 3, 0],[1, 0, 0, 1],[1, 0, 0, 2],[1, 0, 0, 3],[1, 0, 0, 4],[0, 1, 1, 0],[0, 2, 1, 0],[0, 1, 2, 0],[0, 1, 3, 0],[0, 0, 1, 1],[0, 0, 2, 1],[0, 0, 3, 1],[0, 0, 1, 2],[0, 0, 3, 1],[0, 0, 1, 2],[0, 0, 1, 3],[0, 0, 1, 4],[1, 1, 1, 0],[1, 2, 1, 0],[1, 2, 2, 0],[1, 2, 3, 0],[1, 1, 3, 0]...
function arrayIncreaser(a1, a2) {
var pos = 0;
console.log(a1);
breaker = 0;
var fulliteration = 0;
function increase(a1, a2) {
if (pos === a1.length - 1 && a2[pos] === a1[pos]) {
++fulliteration;
a2[pos] = 0;
console.log("one full iteration")
pos = 0;
return
}
if (a2[pos] === a1[pos]) {
a2[pos] = fulliteration;
pos += 1;
a2[pos] += 1;
} else {
a2[pos] += 1;
}
}
function generate(a2) {
if (breaker === 30) {
return
}
++breaker;
console.log(a2);
increase(a1, a2);
generate(a2);
}
generate(a2);
}
arrayIncreaser([1, 2, 3, 4], [0, 0, 0, 0])