I am trying to push objects to an empty array, where its x property value incremented on each loop. I used console.log(B.x) to check that it does iterate but even so, each object will have its x property value set to 300
n = 20, therefore, 100 (original x's value) + 10 (iteration) * 20 (loop) = 300
function createBlocks(n) {
var blockArray = [];
var B = {
x: 100,
y: 100,
width: 35,
height: 35,
color: "brown"
}
for (var i = 0; i < n; i++) {
blockArray.push(B);
B.x += 10;
console.log(B.x);
console.log(blockArray[i]);
}
return blockArray;
}
I don't understand why all the objects have the same value (300) for its x's property.