It looks like you're new here. If you want to get involved, click one of these buttons!
postrender.table = function(table) {
var table = document.getElementById('field');
var rowLength = table.rows.length;
for(var i=0; i<rowLength; i+=1){
var row = table.rows<i>;
var cellLength = row.cells.length;
for(var y=0; y<cellLength; y+=1){
var cell = row.cells[y];
if(cell.innerHTML > 10 && cell.innerHTML == player1Position) {
cell.innerHTML = playerSymbol;
cell.setAttribute("class", "red");}
else if(cell.innerHTML > 10 && cell.innerHTML == obstacle1)
{ cell.innerHTML = "A";
}
else if(cell.innerHTML > 10 && cell.innerHTML == obstacle2)
{ cell.innerHTML = "B";
}
else if(cell.innerHTML > 10 && cell.innerHTML == obstacle3)
{ cell.innerHTML = "C";
}
else if(cell.innerHTML > 10) {
cell.innerHTML = "";
}
}
}
}
Take away the first and the last lines and you'll get what I had initially, which only gave "can't read [i]rows [parameter] of null" as an error. All above the "if" cycles is taken directly from another post on JS and is not examined additionally by me, since I have very little knowledge of JS on my own. Is there a way to fix this and/or use postrender properly to manipulate the table?
Comments
if
statements are doing vile things.Anyway, try this:
How so?
Thanks for the code! It's working just fine. Time for Miners to emerge further.
I was trying to avoid using jQuery, though: DOM is easier for me to understand and respond in. I found out that I've had to repair the Twine variables used within the code (I thought they're stored in the global JS variables, but there were within an object), too, but it's not related to the code you presented.
Repetition of logic and using implicit coercion of the
innerHTML
property. I didn't bother suggesting otherwise before because I assumed that was simply trial code, however, since you've asked.As an example of what I might have suggested before for the contents of the inner
for
-loop: (I'm assuming thatplayer1Position
and the like hold actual numbers)I think I understand now how
innerHTML
could be bad for you. I used the first thing that came into my head, and it worked, so I let it flow. Surely, theNumber(cell.innerHTML)
part is important for the safety of the code, so I'm going to implement it.But what about repetition of logic? I'm confused as to what that even means.
An example of "repetition of logic" would be how you did the same innerHTML > 10 conditional test up to five times in your original version of the code instead of just once like TheMadExile does in his version.