[JS]剰余算 %

by tea4two

HTML

<table id="tbl">
</table>

CSS

.blue th, .blue td {
    background-color: skyblue;
}
.yellow th, .yellow td {
    background-color: yellow;
}

JavaScript

var color,
    arr = {
        'A':'りんご',
        'B':'ごりら',
        'C':'らっぱ',
        'D':'ぱんだ',
        'E':'だん吉'
    },
    tbl = document.getElementById('tbl'),
    counter = 0;

for(var i in arr) { 
    var tr = document.createElement('tr'),
    th = document.createElement('th'),
    td = document.createElement('td');
    
    if( (counter % 2) == 0 ){//交互に
        color = "blue";
    } else {
        color = "yellow";
    }
    tr.setAttribute('class', color);
    var txtNodeTH = document.createTextNode(i),
        txtNodeTD = document.createTextNode(arr[i]);
    th.appendChild(txtNodeTH);
    td.appendChild(txtNodeTD);
    tr.appendChild(th);
    tr.appendChild(td);

    tbl.appendChild(tr);
    counter++;
}