Trying to understand an answer from a previous question.
I found this question and subsequently the answer provided by @obscure, which I was able to modify based on what I needed. It worked perfectly! So I'm just trying to make sure I learn and understand exactly what's happening here.
Best I understand currently:
tempA = fileNames[a]; and tempA = trackTitles[a]; are temporarily storing the current location in the iteration, but I'm not sure what happens at fileNames[tempB] = tempA; and trackTitles[tempB] = tempA;.
tempB = Math.floor(Math.random() * fileNames.length); is generating a random index within the questions array.
fileNames[a] = fileNames[tempB]; and trackTitles[a] = trackTitles[tempB]; are swapping the current index with the randomly generated index.
Any help is appreciated!
this.fileNames = ["fileA", "fileB", "fileC"];
this.trackTitles = ["titleA", "titleB", "titleC"];
function shuffle() {
var tempA;
var tempB;
for (var a = 0; a < fileNames.length; a++) {
tempA = fileNames[a];
tempB = Math.floor(Math.random() * fileNames.length);
fileNames[a] = fileNames[tempB];
fileNames[tempB] = tempA;
tempA = trackTitles[a];
trackTitles[a] = trackTitles[tempB];
trackTitles[tempB] = tempA;
}
}
shuffle();
console.log(fileNames);
console.log(trackTitles);