Strange Array behavior using push and splice - arrays

I have a page full of mc_card's and want the user to choose which ones to add to their deck.
click a card and cardChosen = true for that card;
click again and cardChosen = false;
This works fine.
Upon choosing a card the frame number is stored in an array. Each card is on a separate frame and there are no duplicates.
Main.cardArray.push(this.currentFrame);
Upon clicking it again, I want to remove that frame number from the array:
Main.cardArray.splice(this.currentFrame, 1);
After I splice the array, I trace it, and I'm getting weird results. Sometimes it works like I would expect, but then it removes the wrong numbers and sometimes doesnt remove them at all.

splice() works in another way, that you try to use.
Here is statement:
splice(startIndex:int, deleteCount:uint, ... values):Array
So, first arg - start index in array to delete, and second arg - how much elements must be deleted from the start index.

Related

Dynamic Array of flash AS3

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.
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.

How to iterate over elements in selenium

Actually i want to read emails one by one in junk folder of "outlook:live" and mark emails "Not spam".
emails = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH,"//div[#class = 'xoCOIP8PzdTVy0T6q_uG6']")))
This xpath matches 400 instances. I want to make a loop to select one email at a time like select first email, click on the div and perform action and then 2nd email and so on. I'm trying this
emails = WebDriverWait(driver,
5).until(EC.element_to_be_clickable((By.XPATH,"//div[#class =
'xoCOIP8PzdTVy0T6q_uG6']")))
for count in range(0,len(emails)):
(emails)[count+1].click()
Please help me know where im doing wrong. Thanks in advance
It appears that the function you're using to return the clickable elements is only returning a single element, so you'll have to use a different function, make a change in your logic, etc.
For instance, you could use Selenium's find_elements_by_xpath("//div[#class = 'xoCOIP8PzdTVy0T6q_uG6']") which will return a list of WebElement object(s) if the element(s) are found, or an empty list if the element(s) is not found. This will, of course, not take into consideration the possibility of the elements not being completely loaded on the page. In my experience, just slapping a time.sleep(10) after you open the page is "'good enough".
I recommend making sure your elements can be discovered and interacted with first to make sure this isn't all in vain, if you haven't already.
Another option is to add another function, something like a elements_to_be_clickable() function, to the Expected Conditions source code.
From the Expected Condition documentation, I've done some research and it looks like the element_to_be_clickable() function only returns a single element. Moreover, from the source code, said function mainly makes use of the visibility_of_element_located() function. I believe you could follow similar logic to the element_to_be_clickable() function, but instead use the visibility_of_all_elements_located() function, in order to return multiple WebElements (since visibiilty_of_all_elements_located() returns a list of WebElements).

element.remove() method doesn't work sometimes

I have a test case that shows that angular element.remove() removes elements from the DOM sometimes, and fails miserably at other times even though I don't see an error. Here is the JSFIDDLE.
To see it working, click the Search button (no need to put in any data in the input field). This does two things:
deletes elements above the field and
deletes any elements below the fields (nothing to delete the first time around) and adds new ones.
This is the code that should clear out the elements below the search button.
$scope.searchTargets.forEach(function(target){
var resultNode = angular.element(document.getElementById('id_' + target.name));
if(resultNode != undefined)
resultNode.remove();
Repeatedly clicking on the search shows that the number of elements below the search button keeps increasing - even though it should really be staying at 3 elements. Why does the remove() method fail here?
Take a look at this forked fiddle:
http://jsfiddle.net/wcca93qc/
You need to reset the search results during each search using $scope.searchResults = [];
I also refactored to code a bit, to merge 3 loops that basically can be done in 1 loop.

MATLAB: Matrix Index is out of range for deletion

I have this function where I use an array as a FIFO queue (i.e., put elements in it and process them using a first-in first-served approach). In particular, I call this array a MsgQueue as it holds messages.
The MsgQueue is used when a new msg is sent (event), which triggers the execution of the handleMsgSent() method, which I show next
function handleMsgSent(this, msg)
this.MsgQueue = [this.MsgQueue msg];
while(numel(this.MsgQueue) > 0)
m = this.MsgQueue(1);
this.MsgQueue = this.MsgQueue(2:end); % <----- OPTION A
% DO WHATEVER WITH THE MESSAGE
%this.MsgQueue(1) = []; % <------ OPTION B
end
end
As you can see I have marked the code with OPTION A and OPTION B comment. So, the point is option B ends up with the "Matrix Index is out of range for deletion" error while option A works (apparently) perfectly fine, with no errors.
Can anyone help me to understand the difference? Well, I understand that option A is not deleting anything but just "discarding" the first element of the array but, why does option B fails in deleting if there is at least one element in MsgQueue?
For an empty array, end is 0. If you check the documentation for the colon operator, for start>end it returns an empty array. Thus, the first option deletes noting in some cases while the second option always indexes the second element.
After debugging my code I seem to have realised of the reason. In my code there is a comment:
% DO WHATEVER WITH THE MESSAGE
The problem was that at this point I was invoking another method that triggered new messages to be sent and thus entering the handleMsgSent() method again. Thus, when I modify the MsgQueue before that I have no problems but when I do it afterwards I get into trouble.

AS3 Array Limits?

I've ran into a weird problem with flash, I have an array of 92 buttons, at first it was all contained in a single array, the buttons up to the first 20th buttons work, the rest don't.
The buttons will take the user to the next scene basically.
So I tried to breakup the array into multiple arrays, so the first array contains the first 20, the second array the 21-40th and so on, the fifth array contains the 81-92 buttons.
The problem now is I will get this error message:
TypeError #1010: A term is undefined and has no properties
and it'll break all the buttons, rendering all the buttons unusable.
Therefore, I commented out the
for (var a=0; a<buttons.length; a++)
{
firstarray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
secondarray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
thirdarray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
fourtharray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
//fiftharray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
}
in my button spawn function and the buttons from the first to fourth array works flawlessly well except the fifth, which when clicked, nothing happens.
So I tired to create a new function whereby it was only the fiftharray in it and called the new function in the spawner, same error, breaks everything.
Then I thought was there a button naming issue whereby i mistyped something, I took the button names in the fifth array and pasted them into the start of the fourtharray, replacing what was in it plus commenting out the fiftharray from my script.
The once unworkable buttons (81 to 92) worked, but now (61 to 80) didn't.
I tried combining all the arrays using the comarray, but only the first 20 buttons worked.
So I am wondering if is there a fix or something to solve this problem, much help is appreciated!
There is no need to have multiple arrays, remove them. The last array is obviously shorter than the rest and that's messing up your code -> you are pointing to an index that doesn't exist in your last array.
There is actually no need to have an array. You have 92 buttons, that's a nice bunch. Why not to put it to a movieclip instead? What's the need of the array?
Let's assume you select all your buttons and put them inside of movieclip called buttonsClip. Now you can just use this code, without typing all the instance names out to put them to the array (like the tutorial did it... that may work for 8 buttons, but 92... come on :) ):
import flash.display.MovieClip;
import flash.events.MouseEvent;
for(var i:uint=0; i<buttonsClip.numChildren; i++) {
var b:MovieClip = buttonsClip.getChildAt(i) as MovieClip; //Assuming the buttons are movieclips
b.addEventListener(MouseEvent.CLICK, onClick);
}
function onClick(e:MouseEvent):void {
trace(e.target.name);
}
I know this Question is currently 8 years old, but maybe this will help another person that is searching for the same thing.
I had pretty much the same problem and in my case it was just that I accidently skipped a number while naming all the Symbols in the libary that I wanted to be in this array. I had 42 Symbols but because of this mistake the function to load the array had 43 and obviously the program was confused about that.

Resources