Hello. I was doing this kata in codewars Training on Odd March Bits 8 bits | Codewars
and i don’t understand why i can’t get the desired output. This is my code:
function bitMarch (n) {
let arr = [0,0,0,0,0,0,0,0];
let aux = n;
while(n>=1) {
arr.shift();
arr.push(1);
n--;
}
let container = [];
for(let i = 0; i< arr.length - aux + 1; i++) {
console.log(arr) // this console.log shows me what i want to push,
// i push it but the end result in the container it's allways the same static arr
container.push(arr)
arr.reverse();
// console.log(container) but this .log doesn't showme the same
if(arr[arr.length-1]== 0) {
if(arr[i]==1) {
arr[i+aux]= 1;
arr[i]= 0;
arr.reverse()
}
}
}
return container
}
bitMarch(1);
In the console.log inse the for loop arr it’s showed like this:
[ 0, 0, 0, 0, 0, 0, 0, 1 ]
[ 0, 0, 0, 0, 0, 0, 1, 0 ]
[ 0, 0, 0, 0, 0, 1, 0, 0 ]
[ 0, 0, 0, 0, 1, 0, 0, 0 ]
[ 0, 0, 0, 1, 0, 0, 0, 0 ]
[ 0, 0, 1, 0, 0, 0, 0, 0 ]
[ 0, 1, 0, 0, 0, 0, 0, 0 ]
[ 1, 0, 0, 0, 0, 0, 0, 0 ]
but in the container that i pushed that arr (or i guess i push that arr) return this:
[ 0, 0, 0, 0, 0, 0, 0, 1 ]
[ 0, 0, 0, 0, 0, 0, 0, 1 ]
[ 0, 0, 0, 0, 0, 0, 0, 1 ]
[ 0, 0, 0, 0, 0, 0, 0, 1 ]
[ 0, 0, 0, 0, 0, 0, 0, 1 ]
[ 0, 0, 0, 0, 0, 0, 0, 1 ]
[ 0, 0, 0, 0, 0, 0, 0, 1 ]
[ 0, 0, 0, 0, 0, 0, 0, 1 ]
If someone can help me i will be very grateful. Thanks!