View Full Version : Button not subtracting
raidos
02-01-2010, 02:14 PM
So basically I have a question about subtracting a number that I input into a textbox from a list of numbers from another textbox. When I press the button when i inputted a number it does not subtract from the list of numbers that I earlier inputted.
Im using arrays to store the numbers. This is my code:
function onRemove(event:MouseEvent):void
{
var nIndex:int;
nIndex =(myIntegers.indexOf(IntegersIn_txt.text)); // returns the location of an item or -1 if it is not found.
if (nIndex != -1)
{
for (var i=nIndex; i <myIntegers.length; i++)
{
myIntegers[i] = myIntegers[i+1];
}
myIntegers.pop();
}
timmetoe
02-01-2010, 02:21 PM
Use another function.
the pop() function removes the LAST value in the array.
I think splice() (http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Array.html#splice%28%29) is the function that you need
raidos
02-01-2010, 02:31 PM
Use another function.
the pop() function removes the LAST value in the array.
I think splice() (http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Array.html#splice%28%29) is the function that you need
Tried adding splice instead of pop and no luck it still doesnt remove anything.
The code I gave is suppose to check everynumber on the list and check to see if its there if it is then to would move it down till it's the last number then it would "pop" the list to remove the last number which would be the imputted number. Hope that made sense.
So we assume your work with String and not numbers:
nIndex =(myIntegers.indexOf(IntegersIn_txt.text));
//IntegersIn_txt.text is a string so your are looking for strings in the array correct?
If so just check the value directly:
for (var i=nIndex; i <myIntegers.length; i++)
{
if(myIntegers[i] == IntegersIn_txt.text){
myIntegers.splice(i, 1);//remove the element
return;//exit
};
}
raidos
02-01-2010, 02:44 PM
Thanks ASWC, that seemed to work. Can you tell me why my code did not work? I am just starting out in actionscript and would like to know the errors when im learning.
Hard to say why your code didn't work since I'm not sure what it was supposed to do exactly. If you just want to remove the matching value then no need for a loop:
var nIndex:int;
nIndex =(myIntegers.indexOf(IntegersIn_txt.text));
if (nIndex != -1)
{
myIntegers.splice(nIndex, 1);//remove the element
}
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.