Reputation: 25
I have a task to make a chess desk 8 * 8 using 1 and 0, but I have a problem that the first 7 positions in each array are empty! Why is this happening and how to fix it?
let a13 = [];
function f13() {
for (let i = 0; i < 8; i++) {
for (let k = 0; k < 8; k++) {
let b13 = [];
if ((i + k) % 2 == 0) {
b13[k] = 0;
} else {
b13[k] = 1;
}
a13[i] = b13;
}
}
console.log(a13);
}
f13();
Upvotes: 1
Views: 35
Reputation: 207511
You are defining the array inside of the loop of on every iteration you make it blank. ANd you are setting that array inside the loop so you are replaing it on every iteration. Move it outside
let a13 = [];
function f13() {
for (let i = 0; i < 8; i++) {
const b13 = [];
for (let k = 0; k < 8; k++) {
if ((i + k) % 2 == 0) {
b13[k] = 0;
} else {
b13[k] = 1;
}
}
a13[i] = b13;
}
console.log(a13);
}
f13()
Upvotes: 1