I'm using cordova in combination with coffeescript and I try to shuffle an Array. While debugging why my function does not work, I noticed that an Array I created does not behave as it should. Consider the following piece of code:
shuffleArray: (arr) ->
  $log.log("arr:")
  $log.log(arr)
  arr2 = new Array(arr.length)
  $log.log("arr2 at beginning: " + JSON.stringify(arr2))
  i = 0
  while i < arr.length
    $log.log("i: " + String(i))
    $log.log("ARRAY BEFORE ASSIGNING: ")
    $log.log(arr2)
    valueToAssign = arr[i]
    $log.log("value to assign:")
    $log.log(valueToAssign)
    arr2[i] = valueToAssign
    $log.log("array after assigning: ")
    $log.log(arr2)
    i += 1
  $log.log("arr2 at the end: ")
  $log.log(arr2)
  # ...
When executed with arr=[1,2,3] I get the following result:
[Log] arr: 
[Log] [1, 2, 3] 
[Log] arr2 at beginning: [null,null,null] 
[Log] i: 0 
[Log] ARRAY BEFORE ASSIGNING:  
[Log] [3, 2, 1] 
[Log] value to assign: 
[Log] 1
[Log] array after assigning:  
[Log] [3, 2, 1] 
[Log] i: 1 
[Log] ARRAY BEFORE ASSIGNING:  
[Log] [3, 2, 1] 
[Log] value to assign: 
[Log] 2 
[Log] array after assigning:  
[Log] [3, 2, 1] 
[Log] i: 2 
[Log] ARRAY BEFORE ASSIGNING: 
[Log] [3, 2, 1] 
[Log] value to assign: 
[Log] 3 
[Log] array after assigning: 
[Log] [3, 2, 1]
[Log] arr2 at the end:  
[Log] [3, 2, 1] 
While expecting "ARRAY BEFORE ASSIGNING" to be [null, null, null] the first time there are values yet. Let me state out that this order is random and changes from execution to execution.
So there are two things I cannot figure out:
- Why does arr2 change between when entering the while loop? 
- Why does array value assigning not work? 
And additionally: Why does this not happen when embedded to Android?
Thank you!
 
     
    