Why is this script not behaving like i would expect it to?

function shuf(arr){
for(var i=arr.length-1;i>=1;i--){
  var temp=arr[i];
  var ran=Math.floor(Math.random()*i);
  arr[i]=arr[ran];
  arr[ran]=temp;
  return(arr);
}
}
function build() {

  var arr=[1,2,3,4];
  var arrB=[];
 for(var i=0;i<3;i++){
   arr=shuf(arr);
   arrB.push(arr);
 }
  return arrB;
}

this dosent do what I thought it would.
I thought it would give me an array of shuffled versions of arr.
like

[
[1,2,3,4,5],
[2,1,3,5,4],
[3,5,4,1,2]
]

but it gives me

[
[2,3,1,4,5],
[2,3,1,4,5],
[2,3,1,4,5]
]

it shuffles the first time, but not the rest…
I have no idea why that would be unless for some reason it will only run the shuf() function once inside a loop?

You return immediately in shuf, not after you’re done looping. There may be other problems on top of that.

1 Like

Wouldn’t I want that?
Arr=shuf(are)//arr becomes the value of shuf’s return.
Plus if it were returning after shuf, it wouldn’t push to ar three times

Let me put it differently. shuf() doesn’t do any looping. And the way you set it up, the first iteration doesn’t change anything.