I'm relatively new to working with javascript and currently attempting to enhance the following code snippet (which has been tested and is functioning correctly):
// snail 1 //
var s1 = document.createElement("div");
s1.id = snail1.id;
s1.className = "snail-container";
s1.style.backgroundImage = "url('" + this.snail1.photo+ "')";
s1.style.top = snail1.y + "px";
s1.style.left = snail1.x + "px";
racetrack.appendChild(s1);
// snail 2 //
var s2 = document.createElement("div");
s2.id = snail2.id;
s2.className = "snail-container";
s2.style.backgroundImage = "url('" + this.snail2.foto + "')";
s2.style.top = snail2.y + "px";
s2.style.left = snail2.x + "px";
racetrack.appendChild(s2);
... ... ...
and so forth... (4 in total)
My goal is to convert this into a for loop that can accommodate more instances as needed, however, I keep encountering the error: Uncaught ReferenceError: snail is not defined at window.onload (racerevision.js:138) when I implement the following code:
for(var i = 1; i < 5; i++)
{
var s1 = document.createElement("div");
s1.id = snail+i.id;
s1.className = "snail-container";
s1.style.backgroundImage = "url('" + this.snail.photo+ "')";
s1.style.top = snail+i.y + "px";
s1.style.left = snail+i.x + "px";
racetrack.appendChild(s1);
}
Can you help me identify where my mistake lies in this implementation?