Array = [ 1, 3, 7, 2 ]
1st Iteration = [ 1, 3, 7, 1 ]
2nd Iteration = [ 1, 3, 7, 0 ]
3rd Iteration = [ 1, 3, 6 ]
4th Iteration = [ 1, 3, 5 ]
So on & so forth…
Final Iteration should be an empty []
I’m wanting to create a loop that takes the last index & decrements the value by 1 on each iteration. When the value in the last index = 0 it should pop it…
Any help would be appreciated.
>Solution :
We can use while and Array.prototype.pop().
function solution(array) {
while (array.length !== 0) {
while (array[array.length - 1] >= 1) {
array[array.length - 1] -= 1;
console.log(array);
}
array.pop();
console.log(array);
}
}