I want to position append an image inside a div. The image should be displayed 5 times within a height of 500 and width of 500. They should be positioned randomly be in different places but they should not cross the height and width of the div.But my code produces a result where the images are always positioned in an inclined line right to left and always creating faces which are together.
Here's my code:
var theLeftSide = document.getElementById("leftSide");
var top_position = Math.random() * 500 - 100;
var left_position = Math.random() * 500 - 100;
numberOfFaces = 5;
function generateFaces() {
for (var i = 0; i < 6; i++) {
createElement(i);
numberOfFaces += numberOfFaces;
}
}
function createElement() {
var image = document.createElement('img');
image.src = "smile.png";
image.style.position = 'absolute';
image.style.top = top_position + "px";
image.style.left = left_position + "px";
theLeftSide.appendChild(image);
top_position += 20;
left_position += 20;
}