I have an array list
with all the predefined data I want to work on.
Then I want to make a copy of that array on which I do the work, i.e. shuffling and then popping one element. Now after the list is empty, I want to reset it, i.e. fill it again with the contents of list
.
What I have now is this:
list = [{...}, {...}, {...}]
list2 = list
shuffle = (a) ->
i = a.length
while --i > 0
j = ~~(Math.random() * (i + 1))
t = a[j]
a[j] = a[i]
a[i] = t
a
get_list_item = ->
shuffle(list2)
list2.pop()
reset_list = ->
list2 = list
But after I've popped all the items from list2
, reset_list()
doesn't reset the list. It's still empty
list2 = list
doesn't make a copy of list
, it just creates another pointer to the same array. So when you are using pop()
the original (and only) array loses elements.
Replace these instructions with list2 = list.slice 0
and it should work like you want it to.