This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
en:web_development:lists:javascript [2022/03/13 09:57] mag [Referencing Array Elements] |
en:web_development:lists:javascript [2023/08/16 09:33] (current) |
||
|---|---|---|---|
| Line 4: | Line 4: | ||
| In this exercise you will use an array to store a list of items, use a for loop to repeat a block of code, and use an if statement to run different instructions based on user input. | In this exercise you will use an array to store a list of items, use a for loop to repeat a block of code, and use an if statement to run different instructions based on user input. | ||
| ==== Setup ==== | ==== Setup ==== | ||
| - | * We'll use the first file you created in this lesson called ' | + | * We'll use the first file you created in this lesson called '[[https:// |
| * Create a file called ' | * Create a file called ' | ||
| * We'll also return to [[https:// | * We'll also return to [[https:// | ||
| Line 43: | Line 43: | ||
| * Using this technique, we can display the results of our list in our Visual Studio page. | * Using this technique, we can display the results of our list in our Visual Studio page. | ||
| * Open ' | * Open ' | ||
| - | < | + | < |
| < | < | ||
| </ | </ | ||
| Line 103: | Line 103: | ||
| ==== Iterating Arrays ==== | ==== Iterating Arrays ==== | ||
| * So now that you understand how for loops work, we can apply it to an array. | * So now that you understand how for loops work, we can apply it to an array. | ||
| + | * Open your jsfiddle tab again. | ||
| * Let's edit the for loop you have already. | * Let's edit the for loop you have already. | ||
| < | < | ||
| for (i = 0; i < 10; i++) { | for (i = 0; i < 10; i++) { | ||
| - | text += i + "< | + | text += cars[i] + "< |
| } | } | ||
| document.body.innerHTML = text;</ | document.body.innerHTML = text;</ | ||
| Line 112: | Line 113: | ||
| < | < | ||
| for (i = 0; i < 10; i++) { | for (i = 0; i < 10; i++) { | ||
| - | carList += i + "< | + | carList += cars[i] + "< |
| } | } | ||
| document.body.innerHTML = carList;</ | document.body.innerHTML = carList;</ | ||
| Line 118: | Line 119: | ||
| < | < | ||
| for (i = 0; i < 3; i++) { | for (i = 0; i < 3; i++) { | ||
| - | carList += i + "< | + | carList += cars[i] + "< |
| } | } | ||
| document.body.innerHTML = carList;</ | document.body.innerHTML = carList;</ | ||
| Line 142: | Line 143: | ||
| * Let's see how this works in jsfiddle. | * Let's see how this works in jsfiddle. | ||
| * Add an alert to your Javascript code. | * Add an alert to your Javascript code. | ||
| - | < | + | < |
| * Run that and check that it does what you think it should. | * Run that and check that it does what you think it should. | ||
| * So we can change our continuing condition for the list of players as follows. | * So we can change our continuing condition for the list of players as follows. | ||
| Line 159: | Line 160: | ||
| }</ | }</ | ||
| * The basic structure of this code is < | * The basic structure of this code is < | ||
| - | * We have a condition inside the < | + | * We have a condition inside the < |
| * In this case, our condition is < | * In this case, our condition is < | ||
| * Since we've just defined < | * Since we've just defined < | ||
| Line 207: | Line 208: | ||
| }</ | }</ | ||
| * Run that and try it out for yourself. | * Run that and try it out for yourself. | ||
| + | |||
| + | ==== Final Code ==== | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| [[en: | [[en: | ||