I'm writing a random generator that runs through an array, and randomizes the items in the array, then outputs it to HTML.
The below code works, however I repurposed it from various places and I'd like to understand how this for loop works, specifically, I don't know what for (var c in cars)
checks for, and I've tried simply replacing each instance of cars
in the loop, but it doesn't output anything then.
Here is a functioning codepen: http://codepen.io/npav/pen/xZXxqe
Just the JS:
var cars = 'Tarasenko,Steen,Backes,Stastny,Jaskin,Ott,Brodziak,Upshall,Rattie,Fabbri,Brouwer, Berglund'.split(',');
cars.sort(function() { return 0.5 - Math.random() });
for (var c in cars) {
var newElement = document.createElement('div');
newElement.id = cars[c]; newElement.className = "car";
newElement.innerHTML = cars[c];
document.getElementById("forwards").appendChild(newElement);
}