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);
function build() {

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

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


but it gives me


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.