0

I have an array to store true answers and false answer of random frame multiple choices questions:

var arraytruefalseanswer=[];

I use push method to insert every true and false answer in the array:

arraytruefalseanswer.push(trueanswer)

arraytruefalseanswer.push(falseanswer)

The problem is: I can not remove the last element of arraytruefalseanswer .

Because If I use pop method arraytruefalseanswer.pop(), it will remove all elements in the array arraytruefalseanswer or bring back to arraytruefalseanswer=[]

If i use delete, it is still leaving null.

Please help... how can I remove the last element of arraytruefalseanswer using flash AS3? Thank you.

6
  • (1) You can try updating your array like myArray = myArray.pop(); (2) These are your Array options. Try using slice (startPos, endPos) like myArray.slice(0, (myArray.length-1) ); or try using removeAt (Pos) like myArray.removeAt( myArray.length - 1);... where that myarray is your own array's name and is also updated against itself like in step 1. Commented Sep 6, 2020 at 4:17
  • I've found the answer using visibility method. Thank you for the help so far... n good luck. Commented Sep 6, 2020 at 17:25
  • How does visibility remove item from an array? Also no-one can answer cos you put too much code, why not just show us code of a simple test (make array, put 3 things and show us how you try remove the last thing from that array and we help you to fix). When you know then you use knowledge in your long code. Commented Sep 6, 2020 at 17:31
  • Also I still wonder about this line if(XXX1jumlahbenar1_41BMID1 == XXX1jumlahbenar1_41BMID1++) ... What does it do when it ++ an array? ... You can't say if( myArray == myArray++) { do something }; .. Finally Please put your content inside a MovieClip then travel in those frames like myMC.gotoAndStop(x) because just using gotoAndStop(x) alone makes the whole Stage change frames and you will have errors if your code tries to control something not existing in that frame. Commented Sep 6, 2020 at 17:43
  • "XXX1jumlahbenar1_41BMID1" is a var dynamic text to store the right answer. When clicking the true answer it will add 1 score and the array "myArray" will store "1" as the element in the array. when we don't know and still doubtful with the answer we want to change another answer. Then we click review button. And i want This review button to remove the last element of the array... as i think the only way is using pop() delete, ect. Program still running But giving other result. So, i have an idea why i don't try to manipulating button using visible true and false... and as a result, it works. Commented Sep 7, 2020 at 23:21

1 Answer 1

1

The documentation says: When you do a pop(), it returns items that were popped, and the array gets modified as a side effect. Therefore, in order to just remove the last element, you call arraytruefalseanswer.pop(); as is. You can use trace(arraytruefalseanswer) to verify if anything popped. Also check your code flow, it's possible that when you're popping your last element, you think the code reaches the call once but it's not so, and it say pops your entire array so you've left with an empty array. I can't say more without ever seeing your entire block of code where you work eith your truefalseanswer.

Sign up to request clarification or add additional context in comments.

3 Comments

I've found the answer using visibility method. Thank you for the help so far... n good luck.
@AndiWidodo So you ended up having a XY problem - you claim you have a problem that pop() deletes the array, in reality the code that tries to pop does not have a valid link to the array. OK, at least this helped you to debug the problem.
Yeah.. that time the only way was removing the last element of the array as i think the simplest way...so i tried pop, delete removeAt, splice, slice... From one to another... but no result. Then i tried using visibility and right it is a long way to finish... because it gives side effect to other frames.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.