# Help in array

**URL:** https://forum.kirupa.com/t/help-in-array/351961
**Category:** flash
**Created:** [October 30, 2014, 2:06pm UTC](https://forum.kirupa.com/t/help-in-array/351961 "2014-10-30T14:06:52Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![west\_cost](https://avatars.discourse-cdn.com/v4/letter/w/8edcca/32.png) [@west\_cost](https://forum.kirupa.com/u/west_cost)
#### Post date: [October 30, 2014, 2:06pm UTC](https://forum.kirupa.com/t/help-in-array/351961/1 "2014-10-30T14:06:52Z")

</div>

Hi, I want to work check before displaying any information on what it is to not repeated with supply

My code:  
var sp:Array = new Array( ‘Ball’ , ‘Seq’ , ‘Circle’ , ‘Rec’);  
btn.addEventListener(MouseEvent.CLICK, Press);  
function Press (event:MouseEvent):void  
{  
txt\_1.text = sp[Math.floor(Math.random() \* sp.length)];  
txt\_2.text = sp[Math.floor(Math.random() \* sp.length)];  
txt\_3.text = sp[Math.floor(Math.random() \* sp.length)];  
txt\_4.text = sp[Math.floor(Math.random() \* sp.length)];  
txt\_5.text = sp[Math.floor(Math.random() \* sp.length)];  
txt\_6.text = sp[Math.floor(Math.random() \* sp.length)];  
}

I don’t want to be in the field for similar txt1 with txt2 and txt3 for example…

---

<div class="post-metadata">

### Author: ![west\_cost](https://avatars.discourse-cdn.com/v4/letter/w/8edcca/32.png) [@west\_cost](https://forum.kirupa.com/u/west_cost)
#### Post date: [November 3, 2014, 11:42am UTC](https://forum.kirupa.com/t/help-in-array/351961/2 "2014-11-03T11:42:56Z")

</div>

the problem is the txt6 Empty  
[http://www.gulfup.com/?grjnGk](http://www.gulfup.com/?grjnGk)

and the code is not like I want

because the array Do not be repeated  
only once

I do not mind to be repeated  
like I want “a” in txt1 and txt3 but not in txt2  
I want “b” in txt2 and txt4 but not in txt1

---

<div class="post-metadata">

### Author: ![Trupti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/trupti/32/3418_2.png) [@Trupti](https://forum.kirupa.com/u/Trupti)
#### Post date: [November 3, 2014, 2:29pm UTC](https://forum.kirupa.com/t/help-in-array/351961/3 "2014-11-03T14:29:03Z")

</div>

The code sample I gave you is to generate random set of letter. You can use same with your require number (here 6 letters) of length.  
Use same code to generate random set of 6 letter or whatever text you need…

function randomizeArray(array:Array):Array  
{  
var newArray:Array = new Array();  
while(array.length \> 0)  
{  
newArray.push(array.splice(Math.floor(Math.random( )\*array.length), 1));  
}  
return newArray;  
}  
var myArr:Array = new Array(‘a’, ‘b’, ‘c’,‘d’, ‘e’,‘f’);  
//call function here  
var newRandomArr = randomizeArray(mayArr);

Then update your textfields with values from this array.

Hope this helps…

---

<div class="post-metadata">

### Author: ![west\_cost](https://avatars.discourse-cdn.com/v4/letter/w/8edcca/32.png) [@west\_cost](https://forum.kirupa.com/u/west_cost)
#### Post date: [November 3, 2014, 2:43pm UTC](https://forum.kirupa.com/t/help-in-array/351961/4 "2014-11-03T14:43:12Z")

</div>

but I want “a” in txt1 and txt3 but not in txt2  
I want “b” in txt2 and txt4 but not in txt1  
how?

---

<div class="post-metadata">

### Author: ![Trupti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/trupti/32/3418_2.png) [@Trupti](https://forum.kirupa.com/u/Trupti)
#### Post date: [November 3, 2014, 2:50pm UTC](https://forum.kirupa.com/t/help-in-array/351961/5 "2014-11-03T14:50:46Z")

</div>

You better decide first you need repeat values or not.  
If you are fine with repeat values but you do not want it to be consecutive, then you can just check values for each consecutive field.  
Say, while setting value for txt2, check whether earlier field (txt1) has same value, if it does then replace it with some new value.  
Do the same for all fields.

Hope this helps…

---

<div class="post-metadata">

### Author: ![west\_cost](https://avatars.discourse-cdn.com/v4/letter/w/8edcca/32.png) [@west\_cost](https://forum.kirupa.com/u/west_cost)
#### Post date: [November 3, 2014, 3:24pm UTC](https://forum.kirupa.com/t/help-in-array/351961/6 "2014-11-03T15:24:14Z")

</div>

yes I want to check values for each consecutive field…  
can you just give me example for the code  
and how it works?

---

<div class="post-metadata">

### Author: ![Trupti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/trupti/32/3418_2.png) [@Trupti](https://forum.kirupa.com/u/Trupti)
#### Post date: [November 3, 2014, 3:38pm UTC](https://forum.kirupa.com/t/help-in-array/351961/7 "2014-11-03T15:38:18Z")

</div>

I suggest you understand the logic first,  
I do not have any sample created now but just a quick one is below

//generate random value  
var newRandomValue = Math.round(Math.random()\*6);  
if($(’#txt1’).val() == newRandomValue)  
{  
//get another randomNumber  
newRandomValue = Math.round(Math.random()\*6);  
$(’#txt2’).val(newRandomValue);  
}  
else  
{  
$(’#txt2’).val(newRandomValue)  
}

Same way you can check for other textfield consecutively.  
You can use while loop to check random values.  
Also, you can use for each loop to check and assign value for each textfield.  
Just google for for each to study some sample codes

Hope this helps…

---

<div class="post-metadata">

### Author: ![west\_cost](https://avatars.discourse-cdn.com/v4/letter/w/8edcca/32.png) [@west\_cost](https://forum.kirupa.com/u/west_cost)
#### Post date: [November 3, 2014, 4:04pm UTC](https://forum.kirupa.com/t/help-in-array/351961/8 "2014-11-03T16:04:55Z")

</div>

thank you
