mercredi 2 août 2017

Why doesn't the array store the composite type in a proper way in JavaScript?

I have a function that generates numbers within a range.
I created a composite type like this:

var cowPosition = {
x: 0,
y: 0
};

i also created an array:
var positionsArray = [];
then, i proceed to iterate to fill the array with the composite type.
All of this is inside a function which returns the array.

here's the function:

function generateCowPositions(numberOfCows){
var positionsArray = [];
var cowPosition = {
x: 0,
y: 0
};
var x,y;

for (var i = 0; i < cantidadVacas; i++) {

x = randomPosition(0,5);
y = randomPosition(0,5);

x = x * 80;
y = y * 80;

cowPosition.x = x;
cowPosition.y = y;
positionsArray[i]= cowPosition;

}
return positionsArray;

}

When i run it, it fills the whole array with the last two generated coordinates

Aucun commentaire:

Enregistrer un commentaire