We are dealing with a numerical array where each element has its maximum value. How can we update the elements in order for the array to transition from [0, 0, 0]
to [x, y, z]
?
For example, if the maximum array values are [2, 1, 2]
and the initial array is set at [0, 0, 0]
, the updating process will go through these steps:
[0, 0, 0]
[1, 0, 0]
[2, 0, 0]
[0, 1, 0]
[1, 1, 0]
[2, 1, 0]
[0, 0, 1]
[1, 0, 1]
[2, 0, 1]
[0, 1, 1]
[1, 1, 1]
[2, 1, 1]
[0, 0, 2]
[1, 0, 2]
[2, 0, 2]
[0, 1, 2]
[1, 1, 2]
[2, 1, 2]
I have created a function that halts the updating process when it reaches a maximum of 1. Here is the code snippet:
var maxes = [2, 1, 2];
var myArray = [0, 0, 0];
function step() {
for(var i = 0; i < myArray.length; i++) {
if(myArray[i] == maxes[i]) {
continue;
} else {
myArray[i] = myArray[i] + 1;
return;
}
}
return false;
}
for(j = 0; j < 100; j++) {
result = step();
if(!result) break;
console.log(result);
}