This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
web_design:horario:arrays [2021/10/11 18:55] mag |
web_design:horario:arrays [2023/08/16 09:33] (current) |
||
---|---|---|---|
Line 107: | Line 107: | ||
newRow | newRow | ||
newRow.innerHTML = statsRow;</ | newRow.innerHTML = statsRow;</ | ||
+ | * That's a lot of work for just displaying one line, but we don't have to go through the same thing every time. | ||
+ | |||
+ | ==== Multidimensional Arrays ==== | ||
+ | * We really want our program to run through all our players. So we have a list of players each containing a list of stats. In other words, we have an array of arrays or a multidimensional array. Let's start with the data we already know. | ||
+ | * First, delete the Javascript code we made above. | ||
+ | < | ||
+ | players = [ | ||
+ | [' | ||
+ | [' | ||
+ | ['Juan Guillermo', | ||
+ | [' | ||
+ | ];</ | ||
+ | * Note that each line is an element in the larger list, so we separate them by < | ||
+ | * The only < | ||
+ | * So we are saying that we have an array of arrays (list of lists) called < | ||
+ | * We want to show each player in turn, so we need data from < | ||
+ | < | ||
+ | for (p = 0; p < players.length; | ||
+ | }</ | ||
+ | * It's called a loop because it will loop (iterate) through any code inside the < | ||
+ | * There are a few ways to do this, but we'll start with a < | ||
+ | * Inside the < | ||
+ | * The first < | ||
+ | * The third says that each time the loop finishes, we should increment p (add 1). | ||
+ | * The second says to keep going while < | ||
+ | * Now, for each player, we need to create the statsRow as for the single player. | ||
+ | * We no longer have the variable called ospinaRamirez, | ||
+ | < | ||
+ | statsRow | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | * Place this code inside the for loop between the < | ||
+ | * Finally, we can add the row to the table. | ||
+ | < | ||
+ | newRow = tableBody.insertRow(p); | ||
+ | newRow.innerHTML = statsRow;</ | ||
+ | * Notice that we've changed the < | ||
+ | * There' | ||
+ | * Our final code looks like this. | ||
+ | < | ||
+ | [' | ||
+ | [' | ||
+ | ['Juan Guillermo', | ||
+ | [' | ||
+ | ]; | ||
+ | | ||
+ | tableBody = document.getElementsByTagName(" | ||
+ | |||
+ | // loop through all players | ||
+ | for (p = 0; p < players.length; | ||
+ | // create stats string | ||
+ | statsRow | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | statsRow += "< | ||
+ | | ||
+ | // add row to table | ||
+ | newRow = tableBody.insertRow(p); | ||
+ | newRow.innerHTML = statsRow; | ||
+ | }</ | ||
+ | * This looks a lot more efficient now, but you still might think that it's easier to create the table in HTML than Javascript. Ok. What about if we use the full team? Can you imagine building the table with 26 players? Creating the array is much easier. | ||
+ | |||
+ | [[web_design: |