0
The function shuffle
receives as input a array and the shuffle.
//
function shuffle(a) {
// atribui a variável n a quantidades de elementos da array a.
n = a.length;
// Percorre a array a da posição 0 até a ultima posição.
for (var i = 0; i < n; i++) {
//Sorteia uma posição aleatória entre i e n−1 e atribui para a variável swap
swap = i + Math.floor(Math.random() * (n - i - 1));
// Troca os valores das posições i e swap da array a.
var aux = a[i];
a[i] = a[swap];
a[swap] = aux;
}
}
var array = [5, 3, 1, 4, 2];
if (shuffle(array)) console.log(array);
There are potential pitfalls (pitfalls) in the above function? If YES what is it or what are they? Comment on the answer.
I had it executed and it didn’t work.
What is your question in response to the exercise?
– Leandro Angelo
tried to run and failed and did not understand the function functioning
– Alberto Verzemiassi Borguesani
Your if is preventing you from showing the output. Take out the if and run the direct function. So try to understand how it works and what the potential pitfalls might be.
– bfavaretto
@Albertoverzemiassiborguesani Has the answer solved your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).
– Maniero