dimanche 30 novembre 2014

javascript random in a loop


I have a loop that generates a random number between 0 and 5 and goes fetch a color in an array.


The first color is always yellow and I cannot figure out why.





var hexColors = new Array("#000000", "#FFFFFF", "#FF0000", "#0000FF", "#00FF00", "#FFFF00");
var nameColors = new Array("White", "Red", "Blue", "Black", "Green", "Yellow");

for (var i = 5; i >= 0; i--) {
var hexColor = randomNum(i);
var nameColor = randomNum(i);
$('td:eq(' + i + ')').css("color", hexColors[hexColor]);
$('td:eq(' + i + ')').html(nameColors[nameColor]);
hexColors.splice($.inArray(hexColors[hexColor], hexColors), 1);
nameColors.splice($.inArray(nameColors[nameColor], nameColors), 1);
}

function randomNum(max) {
return Math.floor(Math.random() * max);
}



<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</table>



I re-factored the loop so that it uses increments instead of i-- and it works but I'd still like to know why it doesn't work the other way around.





Aucun commentaire:

Enregistrer un commentaire