Changing data in array with substr in loop - arrays

I have a list of words in myArray, I need to make them all have 12 symbols, no more, no less. So first of all I weed out all words shorter than 12 symbols. IT WORKS. But then I must to cut extra symbols in words longer than 12 symbols. I use array12[j].substr(0,12); and its not working.
My question is: How to change data in array "on the fly" - in a loop? Trace I use after substr returns me same array long words, without cut.
function myArrayLoopFunction()
{
for (var i:int = 0; i < myArray.length; i++) // this works fine
{
if (myArray[i].length >= 12) {
array12.push(myArray[i]);
}
}
for (var j:int = 0; j < array12.length; j++)
{
if(array12[j].length > 12 )
{
array12[j].substr(0,12); //doesnt work
trace(array12[j]);
}
}
}

You can do it in one loop. I think your mistake is that you're not assigning the result of the substr method back to your array:
function myArrayLoopFunction()
{
for (var i:int = 0, str:String; i < myArray.length; i++)
{
str = myArray[i];
if (str.length >= 12) {
myArray[i] = str.substr(0, 12);
}
}
}

You're not assigning the substring to anything.
array12[j].substr(0,12);
should be
array12[j] = array12[j].substr(0,12);

Related

Google App Script breaking singular element into 1D array with several elements

I've created a custom script in GAS (for Google Sheets) so I could join several data sources into one unique display. I added a .splice in the middle so I could cut out all null elements inside the arrays (which meant removing blank rows in the return array).
Here is it how the code goes:
function COMPILER(){
var i;
var c;
var r;
var display = [];
for (i = 0; i < arguments.length; i++) {
for (c = 0; c < arguments[i].length;c++) {
display.push(arguments[i][c]);
};};
for (r = display.length-1; r >= 0; r--) {
if (display[r][1]==""){
display.splice(r, 1)
};};
return display
};
The code works fine with 2+D arrays and with 1D arrays that have 2+ elements; but when its working with 1D arrays that have only one element, it unintendedly breaks down the strings into several elements.
Instead of returning:
ar1
ar2
It returns
a
r
1
a
r
2
How could I solve this?
I think/assume this is what you're trying to do:
function COMPILER()
{
var display = [];
for(var i = 0, numArguments = arguments.length; i < numArguments; ++i)
{
//console.log([i, arguments[i], Array.isArray(arguments[i])]);
if(Array.isArray(arguments[i]))
{
for(var j = 0, len = arguments[i].length; j < len; ++j)
{
if(arguments[i][j] != "")
{
display.push(arguments[i][j]);
}
}
}
else if(arguments[i] != "")
{
display.push(arguments[i]);
}
}
return display;
}
I can't confirm/test cause I don't know how you're calling COMPILER.

Adding a fixed number of unique items to an array using a for loop and indexOf

I'm using a for loop to get 6 random numbers between 1-20, using indexOf to omit duplicates, and pushing them to an array.
However, I always want 6 items in the array, so I'd like duplicates to be replaced. In my naive code duplicates are simply omitted, which means that sometimes I'll get less than 6 in the array. How do I replace the omissions to fill those 6 array slots?
function rolld(event:MouseEvent) {
for (i = 0; i < 6; i++){
d = (Math.floor(Math.random() * (1 + d_hi - d_lo)) + d_lo);
if (rollArray.indexOf(d) < 0){
rollArray.push(d);
}
}
trace (rollArray);
}
Still very new to this. Thanks for any help!
When you get an element from array with 20 elements, try to remove it from array.
function rolld(event:MouseEvent) {
var elements:Array = [];
for (var i:int = 1; i <= 20; i++)
{
elements.push(i);
}
for (i = 0; i < 6; i++){
d = (Math.floor(Math.random() * elements.length);
rollArray.push(d);
//remove the element
elements.splice(d, 1);
}
trace (rollArray);
}

How to remove child in an array in actionscript

How to remove a child in an array in ActionScript 3.0?
Here's my code. I put the child I want to remove in an array called nailList[] and put it on gameStage.
function makeNails():void
{
for (var i=0; i< 3; i++)
{
for (var j=0; j<3; j++)
{
var tempNail = new Nail2(j * C.NAIL_DISTANCE + C.NAIL_START_X,
i * C.NAIL_DISTANCE + C.NAIL_START_Y);
nailList.push(tempNail);
gameStage.addChild(tempNail);
}
}
gameStage.addEventListener(Event.ENTER_FRAME,update);
gameStage.addEventListener(MouseEvent.CLICK, clickToHit);
}
Here's the code I used to remove the nails. But nothing happens after this code. The nails are still there.
for (var j:int = nailList.length; j >= 0; j--)
{
removeNails(j);
trace("Remove Nail");
}
function removeNails(idx:int)
{
gameStage.removeChild(nailList[idx]);
nailList.splice(idx,0);
}
I want to remove the MovieClip so that I can restart the game and add new ones.
Valid array index range is from 0 to length - 1. You are trying to call removeChild with nailList[nailList.length] which is invalid and removeChild should fire an error. You need to change the initial value of j to nailList.length - 1.
for (var j:int = nailList.length - 1; j >= 0; j--)
^^^
Another problem is (as pointed in the comment) the second parameter of splice is delete count. So you need to use 1 instead of 0.
nailList.splice(idx, 1);
^
You can use splice to remove the child of an array.
arrName.splice(2,1);
Hope it will help.

AS3: How to check if a value already exists in the Array before adding with FOR loop?

I believe is something simple but obviously not simple enough :). Any ideas how to check if a value already exists in the Array before adding the value using FOR loop?
I have this so far and it doesn't work as I want to because the Array can contain duplicate values!
var n:int = 5;
var cnt:int;
for (var i = 0; i < n; i++)
{
cnt = randomThief();
for (var a = 0; a < loto5.length; a++)
{
if (loto5[i] == cnt)
{
loto5[i] = cnt;
}
}
}
You can use the indexOf() method of the Array class to check if the value exists like this :
var index:int = loto5.indexOf(cnt);
indexOf() returns a -1, if the value doesn't exist. Here is an example of how to do a check :
if (loto5.indexOf(cnt) >= 0)
{
// do something
}
for (var a = 0; a < loto5.length; a++)
{
cnt = randomThief();
if (loto5.indexOf(cnt) == -1) //if cnt isn't in array do ...
{
trace (cnt+" is not in Array");
loto5[a] = cnt;
}
}
Works, simple and beauty :)

input an array output bigger array related to input

suppose I have a arrays from each i want to produce b these are just examples
a=[4]=> b=[0,4]
a=[3,1]=>b=[0,3,3,4]
a=[2,2]=>b=[0,2,2,4]
a=[2,1,1]=>b=[0,2,2,3,3,4]
a=[3,4,2,5]=>b=[0,3,3,7,7,9,9,14]
I mean when getting 4 it should produce from 0 and then add it to it's content for example 4
or in a[2,1,1] first it will produce 0 and then it see that the next one in a is 1 so after again producing it it will compute 2+1 and assign it.so the output always will be twice size of the input.
i want a pseudo code for it my problem is that when it will repeat I can not write it.
I used JavaScript like syntax.
var a = new Array(3,4,2,5);
var b = new Array();
var bArrayIndex = 0;
b[bArrayIndex] = 0;
bArrayIndex++;
for(i = 0; i < a.length; i++) {
b[bArrayIndex] = b[bArrayIndex-1] + a[i];
if(i < a.length - 1) {
b[bArrayIndex+1] = b[bArrayIndex];
}
bArrayIndex+=2;
}
for(i = 0; i < b.length; i++) {
document.write(b[i] + " ");
}

Resources