I am new to Javascript and therefore have a simple question. I am trying to programme a for loop within which new variables can be created using the i value. How can I refer to i to change the variable names (without using an array)? In the example below I want to create top1, top2, left1, left2 etc.
var i;
for (i=1; i<3; i++) {
var top'i'=Math.random(); top'i'=450*top-150;
var left'i'=Math.random(); left'i'=left*1150;
document.getElementById("image'i'").style.top=top'i'+"px";
document.getElementById("image'i'").style.left=left'i'+"px";
document.getElementById("image'i'").style.display="block";
}
You can use
Simply, at each iteration you will override the values of the previous iteration.