so I'm currently making a quiz game with Actionscript 3.0, and I wanna shuffle the questions with this Fisher-Yates Shuffle Algorithm:
This is my code:
var questions:Array = [1,2,3,4,5,6,7,8,9,10];
function ShuffleArray(input:Array)
{
for (var i:int=input.length-1; i>=0; i--)
{
var randomIndex:int = Math.floor(Math.random() * (i+1));
var itemAtIndex:int = input[randomIndex];
input[randomIndex] = input[i];
input[i] = itemAtIndex;
}
}
function buttoncorrect(event:MouseEvent):void
{
ShuffleArray(questions);
trace (questions);
var quest:int = questions.splice(questions, 1)[0];
trace(quest);
}
btntry.addEventListener(MouseEvent.CLICK,buttoncorrect);
And here is the trace result:
9,7,5,10,4,8,6,2,1,3
9
7,1,2,6,8,10,5,4,3
7
5,2,3,10,6,1,8,4
5
4,3,10,6,2,8,1
4
2,1,8,3,6,10
2
10,8,6,1,3
10
3,8,6,1
3
1,8,6
1
6,8
6
8
0
Why do I always get the "0" value at the end? I should've get the value of "8" right?
Does someone know what's wrong with my code? Thank you.
The problem is this line:
var quest:int = questions.splice(questions, 1)[0];
The first parameter to splice() is supposed to be a start index. Since you want to remove the first element, it should be zero:
var quest:int = questions.splice(0, 1)[0];
The reason your code works for each case except the last one is because of the way automatic type conversion from Array to int works. ActionScript is automatically converting the array passed as the first parameter to a zero when it has more than one element, but converts it to the value of the only element in the array when it has a single element. So it is effectively:
var quest:int = questions.splice(0, 1)[0];
right up until the last call, when you pass in an array containing the single value 8. Then the last time it gets called, you are effectively doing this:
var quest:int = questions.splice(8, 1)[0];
Since that index is beyond the end of the array, an empty array is returned and traces out as 0.
Related
I'm trying to figure out how I can access an item in an array by it's index number. I've written a script which will generate an array, with some number of variables. There's an old script on the imageJ mailing list archive (shown below) which is able to print given index value for a known value within the array, but is there a way to find the value within the array itself? I.e. if I have the user input the number of values that should be in the array, can I have the macro call up the values in the array from that?
My array generator:
Dialog.create("Time Point Input");
Dialog.addNumber("How many time points?", 0)
Dialog.addString("What are your time points (comma separated, no spaces)?:",0);
Dialog.show();
time = Dialog.getNumber();
points = Dialog.getString();
Fpoints = newArray(points);
Where the readouts could be something like:
time = 4
points = 5,10,12,27
Fpoints[0] = 5
Fpoints [1] = 10
Fpoints [2] = 12
Fpoints [3] = 27
Calling up index from array number value example code:
arr = newArray(1,5,3,12);
i = index(arr, 5);
print("index = "+i);
function index(a, value) {
for (i=0; i<a.length; i++)
if (a[i]==value) return i;
return -1;
}
Thank you!
I am not 100% sure if I get your question right.
but is there a way to find the value within the array itself?
The problem is that you cannot create an Array with points since it's a String.
Try something linke:
Fpoints = split(points, ',');
Then you can iterate over Fpoints with a loop, or use you index function to get an index of a given value.
for (i = 0; i < Fpoints.length; i++) {
print(Fpoints[i]);
}
var word = "morning"
var arr = Array(word)
for s in 0...word.count {
print(arr[s])
}
This will not print. Of course, if I substitute a number for s, the code works fine.
Why will it not accept a variable in the array access braces? Is this peculiar to Swift?
I've spent a long time trying to figure this out, and it's nothing to do with s being optional.
Anyone understand this?
you are using inclusive range ... instead of ..<, so s goes from 0 to 7, not 0 to 6.
However, in arr the index goes from 0 to 6 because there are 7 characters.
Thus, when the program tries to access arr[7], it throws an index out of range error.
If you were coding on Xcode, the debugger would have told you that there is no arr[7].
As for the code, here is a better way to print every item in arr than using an index counter:
var word = "morning"
var arr = Array(word)
for s in arr {
print(s)
}
This is called a "foreach loop", for each item in arr, it assigns it to s, performs the code in the loop, and moves on to the next item, assigns it to s, and so on.
When you have to access every element in an array or a collection, foreach loop is generally considered to be a more elegant way to do so, unless you need to store the index of a certain item during the loop, in which case the only option is the range-based for loop (which you are using).
Happy coding!
When I run it, it prints the array then throws the error Fatal error: Index out of range. To fix this, change the for loop to:
for s in 0..<word.count {
print(arr[s])
}
try this
when you use a word to recognize
size of Array your Array index start as 0 so array last index must be equal with your (word.count - 1)
var word = "morning"
var arr = Array(word)
for s in 0...(word.count-1) {
print(arr[s])
}
Basically avoid index based for loops as much as possible.
To print each character of a string simply use
var word = "morning"
for s in word { // in Swift 3 word.characters
print(s)
}
To solve the index issue you have to use the half-open range operator ..< since indexes are zero-based.
Hello could anyone tell me how to solve this one, it's so simple but I'm new in programming.
f([9,8,6,2])
For what it's worth, the function is looping through the given array [9,8,6,2] and adding the numbers together. It's essentially a sum of the values of the given array.
The var s is initially zero (0) (int s = 0). It loops through each value in the array and adds it to itself (s = s + arr[i]). As the loop continues, the value of s continues to grow.
Therefore, the returned value of the function would be 25:
s = 9 + 8 + 6 + 2
I have an array in ActionScript 3 that is formatted as follows:
var _arrayList = new Array;
_arrayList["item1"] = true;
_arrayList["item2"] = 56;
_arrayList["item3"] = "john doe";
_arrayList["item4"] = -56.8;
and I'm attempting to pull the "_arrayList.Length" but it comes back 0... why?
Because the way you do it makes the array "associative". In Action Script, every class is a basic Object, with dynamic properties. And array does both - containing indexed elements, and having a dynamic properties.
Here's an example of a regular array:
var array:Array = new Array();
array.push(value); // equal to array[0] = value;
array.push(56); // equal to array[1] = value;
array[20] = 'test'; // will put 20th element to be 'test'
If you use this approach, the length of the array will not be 0. And so you can use a for loop:
var total:uint = array.length;
for (var i:uint = 0; i < total; i++) {
trace (array[i]);
}
But when the keys are not numeric, you are actually setting "properties" to the array and thus the length is 0. Again, you can loop through them, but with for-in:
for (var key:String in array) {
trace (array[key]);
}
Here the keys won't be 0, 1, 2, 3 and so on, but instead will be item1, item2, item3. (keep in mind that the order is not preserved!)
So the basic conclusion is that if you use non-numeric keys, then use an Object instead. Array is for numeric indexed collections.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the best way to get the minimum or maximum value from an Array of numbers?
Hi - I'm trying to use flash's Math.max() functionality to find the highest of a set of numbers. Normally, these are input via a comma delimited string of numbers, but I need to have it loop through a series of numbers in an array. What is the proper syntax for this? I tried:
var maxMemberWidth = int(Math.max(
for (var k=0;k<memberClips.length;k++){
memberClips[i].memeberWidth;
}
));
but that's obviously not right. I have a feeling the answer involves running some sort of separate function and returning the values to this function, but I haven't quite gotten the syntax right yet.
Your aproach is not ALL wrong, you can do this:
// note the apply in the next line!
var maxMemberWidth:int = int(Math.max.apply(null, AllMembersWidth()));
...
private function AllMembersWidth():Array
{
var widths:Array = [];
for (var k:int = 0; k < memberClips.length; k++)
widths[k] = memberClips[k].memeberWidth;
return widths;
}
This works even when you have an empty array (it returns -Infinity)
You can try this instead:
var max_number:Number = memberClips[0];
for( var i:int = 1; i < memberClips.length; i++ )
{
max_number = Math.max( max_number, memberClips[i] );
}
What this does is initializes the maximum number to the first element of your array and then loops through all the elements in your array until it reaches the end of the array. You'll then have the maximum number you're seeking. The index variable i is initialized to 1 because you don't need to compare the first element against itself. You should take note that this code also assumes that you have at least one element in your array.
More condensed version, not as sexy looking as it would be on ruby but it works.
var widths:Array = clips.map( function(o:*, i:int, a:Array):* { return o.width; } );
var max:Number = Math.max.apply(null, widths);
var min:Number = Math.min.apply(null, widths);