Skip to main content
Participant
September 18, 2013
Answered

Issue with 'MyArray.splice'?

  • September 18, 2013
  • 1 reply
  • 702 views

I have the following code in my ActrionScript 3.0 class:

if(RandTarget == true){

     for(b = 0; b < totalHits; b++){

          tempArray = tempArrayB.slice(0);

          hitsPerTarget[tempArray.splice(Math.floor(Math.random() * tempArray.length), 1)] += 1;

     }

}   

This code is intended to set tempArray equal to tempArrayB, then add 1 to a specific spot on the hitsPerTarget array, which is determined randomly based on which number is spliced out of tempArray.

Using Trace, it appears that two elements are deleted from the hitsPerTarget array during each cycle of the for loop, instead of just one. However, if I change the "+= 1" part to just "= 1", it goes back to just deleting one element like it's supposed to. Why is this, and how can I fix it? I need this loop to add 1 to the chosen spot on the array for each cycle of the loop, and splice out no more than 1 one the elements in the array with each cycle.

This topic has been closed for replies.
Correct answer Ned Murphy

There are likely a couple of things at work here that are not obvious.  The first being that when you splice an array it returns an array.  So the following code is not properly devised...

    hitsPerTarget[tempArray.splice(Math.floor(Math.random() * tempArray.length), 1)]

since it is really specifying...

    hitsPerTarget[Array] 

where I think what you really want is just the 0 element within that array.

I think (I'm not certain) the part that does the double removal from the array is the use of the += with some processing hierarchy working behind the scenes.

A += B is the same as saying

A = A + B

So what if it actually performs that math in that manner behind the scenes?  What if A is some set of functions....

A() = A() + B

Now replace that A function with your splicing method code... it ends up splicing the array twice.

What I recommend for avoiding this is to do that splicing before trying to use it in the += line, and remember that you are dealing with a spliced array... as in....

   var temp:Array = tempArray.splice(Math.floor(Math.random() * tempArray.length), 1);

   hitsPerTarget[temp[0]] += 1;

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
September 18, 2013

There are likely a couple of things at work here that are not obvious.  The first being that when you splice an array it returns an array.  So the following code is not properly devised...

    hitsPerTarget[tempArray.splice(Math.floor(Math.random() * tempArray.length), 1)]

since it is really specifying...

    hitsPerTarget[Array] 

where I think what you really want is just the 0 element within that array.

I think (I'm not certain) the part that does the double removal from the array is the use of the += with some processing hierarchy working behind the scenes.

A += B is the same as saying

A = A + B

So what if it actually performs that math in that manner behind the scenes?  What if A is some set of functions....

A() = A() + B

Now replace that A function with your splicing method code... it ends up splicing the array twice.

What I recommend for avoiding this is to do that splicing before trying to use it in the += line, and remember that you are dealing with a spliced array... as in....

   var temp:Array = tempArray.splice(Math.floor(Math.random() * tempArray.length), 1);

   hitsPerTarget[temp[0]] += 1;

Participant
September 18, 2013

I see. Thanks a lot for the detailed help. I managed to solve the problem using your example. Since I was only having the splice return one element, I hadn't considered that the data returned would be an array. I'll remember that now.