这可以重写为:
// loop from arrayArr.length - 1 to 0
for (var randomIndex, temp, i = answerArr.length - 1; i >= 0; i--)
{
// get a random index in the array.
randomIndex = Math.floor(Math.random() * i);
// put the current index in a temporary variable
temp = answerArray[i];
// assign the random index to the current index
answerArr[i] = answerArr[randomIndex];
// assign the temporary variable to the random index
answerArr[randomIndex] = temp;
}
// now output the new shuffled array
for(var t = 0; t < answerArr.length; t++)
{
$("#kc_answers").append('<li><span class="kc_answer_span">' + $(answerArr[t]).find('aText').text() + '</span></li>');
}
更新
关于for loop
没有身体,基本上这是 a 的for loop
工作方式:
for (run what is in here once;
evaluate this each time after the next statement and the body... if it evalutes to false then exit;
run what is here each time after the body)
{
// run this until the second statement is false
}
所以原代码的作者是这样做的:
for (run this once;
evaluate this expression each time;
forget having a body, just put everything in here!!);