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

**URL:** <https://forum.freecodecamp.org/t/why-is-this-script-not-behaving-like-i-would-expect-it-to/177155>\
**Category:** General\
**Created:** [February 28, 2018, 12:08pm UTC](https://forum.freecodecamp.org/t/why-is-this-script-not-behaving-like-i-would-expect-it-to/177155 "2018-02-28T12:08:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![woodsrlee](https://avatars.discourse-cdn.com/v4/letter/w/ba8739/32.png) [@woodsrlee](https://forum.freecodecamp.org/u/woodsrlee)\
**Post date:** [February 28, 2018, 12:09pm UTC](https://forum.freecodecamp.org/t/why-is-this-script-not-behaving-like-i-would-expect-it-to/177155/1 "2018-02-28T12:09:00Z")

</div>

```auto
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

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

```

but it gives me

```auto
[
[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?

---

<div class="post-metadata">

**Author:** ![lynxlynxlynx](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lynxlynxlynx/32/2597_2.png) [@lynxlynxlynx](https://forum.freecodecamp.org/u/lynxlynxlynx)\
**Post date:** [February 28, 2018, 12:35pm UTC](https://forum.freecodecamp.org/t/why-is-this-script-not-behaving-like-i-would-expect-it-to/177155/2 "2018-02-28T12:35:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![woodsrlee](https://avatars.discourse-cdn.com/v4/letter/w/ba8739/32.png) [@woodsrlee](https://forum.freecodecamp.org/u/woodsrlee)\
**Post date:** [February 28, 2018, 1:00pm UTC](https://forum.freecodecamp.org/t/why-is-this-script-not-behaving-like-i-would-expect-it-to/177155/3 "2018-02-28T13:00:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lynxlynxlynx](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lynxlynxlynx/32/2597_2.png) [@lynxlynxlynx](https://forum.freecodecamp.org/u/lynxlynxlynx)\
**Post date:** [February 28, 2018, 1:15pm UTC](https://forum.freecodecamp.org/t/why-is-this-script-not-behaving-like-i-would-expect-it-to/177155/4 "2018-02-28T13:15:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:25pm UTC](https://forum.freecodecamp.org/t/why-is-this-script-not-behaving-like-i-would-expect-it-to/177155/5 "2021-01-17T20:25:40Z")

</div>

Closed on January 17, 2021, 8:25pm UTC


