This is an old revision of the document!
cars = ['Ferrari', 'Porsche', 'Lamborghini'];
cars
is the name of a variable and it now holds everything after the =
sign. []
identifies an array, in this case holding a list of cars. '
signs.document.getElementsByTagName('span')[2]
. document.getElementsByTagName
is a list of all elements with the tag 'span'. [2]
means that we access the 3rd item. Remember that the first is [0]
.for (i = 0; i < 10; i++) { }
for
is the function, which will run code inside the {}
a number of times according to the rules inside ()
. ;
. i
, short for 'iterator'.i = 0
means that the first time we run the code, the variable i
will have a value of zero.i < 10
.i++
is the same as i = i + 1
so each time we loop through the code, we'll increment the variable i
. So it will run with a value of i incrementing from 0 to 9. i
. {}
.alert("i = " + i);
i
. //
. {}
.text += i + "<br />"; document.body.innerHTML = text;
text
is a variable we'll use to hold the text to be displayed. +=
means that we'll add the new text to the full string we're building. i
and a <br>
tag which adds a new line.document.body.innerHTML
refers to the entire content (body
) of the HTML document.text
) to the HTML body.text
is the first time it sees it.var text = "";
text
is a variable (var
of type String ( ""
).var text = ""; for (i = 0; i < 10; i++) { text += i + "<br />"; } document.body.innerHTML = text;
var carList = ""; for (i = 0; i < 10; i++) { carList += i + "<br />"; } document.body.innerHTML = carList;
i = 0
and iterating statement i++
are still valid, but we need to adjust the continuing condition. i < 3
, but you may have added more cars to your list. You could change the number directly to match your list, but in the future you'll have lists pulled from databases and you won't always know exactly how many elements are in the list. .length
, so let's change the for loop to the following.var carList = ""; for (i = 0; i < cars.length; i++) { carList += i + "<br />"; } document.body.innerHTML = carList;
i
.var carList = ""; for (i = 0; i < cars.length; i++) { carList += cars[i] + "<br>"; } document.body.innerHTML = carList;
carList += "<li>" + cars[i] + "</li><br>";
<ul> </ul>
<ul>
tag.document.getElementsByTagName('ul')[0].innerHTML = carList;
<head>
to link to the external style sheet, we need to tell the browser where to find our javascript code.<head>
and type 'script', then select 'script:src'. src
attribute.<script src="lists.js"></script>
players = ['David Ospina Ramirez', 'Johan Andres Mojica Palacio', 'Gustavo Leonardo Cuellar Gallego', 'Duvan Esteban Zapata Banguera'];
player
is the name of a variable and it now holds everything after the =
sign. []
identifies an array, in this case holding a list of players. '
signs. ,
.