function randOrd(){ return (Math.round(Math.random())-0.5); }
var cardArray = [
[1, "hundo", "dog"], [2, "kato", "cat"], [3, "ŝafo", "sheep"]
];
randomCardArray = cardArray.sort( randOrd );
// Copy esperanto and an index number from original array to a new one.
var eoArray = new Array();
for(i=0;i<randomCardArray.length;i++){
eoArray[i] = randomCardArray[i][0] + randomCardArray[i][1];
}
// Copy english and an index number from original array to a new one.
var enArray = new Array();
for(i=0;i<randomCardArray.length;i++){
enArray[i] = randomCardArray[i][0] + randomCardArray[i][2];
}
document.write("Eo= " + eoArray + '<br />');
document.write("En= " + enArray[1][1] + '<br />');
Which gives this...
Eo= 3ŝafo,2kato,1hundo
En= 3sheep,2cat,1dog
...but if I wanted to access enArray[0][1] I would expect 'sheep', but instead I get 's'.
How do I copy part of the array like I did in enArray, but not lose the formating of the originl cardArray?
It would be easier to use an Arrays of Objects instead of a double Array. It is more simple and more intuitive.
Code:
var cardArray = [
{"hundo":"dog"},
{"kato":"cat"},
{"ŝafo":"sheep"}
];
More about Objects and Arrays in JSON: www.json.org
On the other hand: why do you need an intermediate random operation, as long as you finally sort the array?
It would be easier to use an Arrays of Object instead of a double Array. It is more simple and more intuitive.
Code:
var cardArray = [
{"hundo":"dog"},
{"kato":"cat"},
{"ŝafo":"sheep"}
];
More about Objects and Arrays in JSON: www.json.org
On the other hand: why do you need an intermediate random operation, as long as you finally sort the array?
Yep, the JSON looks nicer. I've just never had a chance to 'think' in it yet.
I guess I randomised it twice because I thought it might make it more random (lol).
The final code will be a Match the card game style game, which is why I have the numbers there -- so I have some way to keep check for matches. Not sure, if that was the best way to go about it though. I can post a link if you are interested?
if my hunch is correct, the following snip should return a value close to 50, but it's consistently above 70, and i've never seen it under 50.
the code compares 100 arrays sorted by your randOrd function, just as you do it.
the value of the array don't matter, so the ordering will be related for any array.
after collecting 100 shuffled arrays, i count the number of times the first 4 digits are lower than the last four digits.
in a random array, that should be true about half the time.
but it's true a lot more than chance alone, meaning that the big values are staying on the right.
i don't see how the sort function would ever return zero, so it only swaps half the time...
Code:
function randOrd(){ return (Math.round(Math.random())-0.5); }
var r=[1,2,3,4,5,6,7,8,9];
var rs=[];
for(var i=0;i<100;i++){
rs.push(r.concat().sort(randOrd));
}
rs.filter(function(a){
var left= a[0]+a[1]+a[2]+a[3];
var right=a[5]+a[6]+a[7]+a[8];
return left < right;
}).length //should be about 50...
Bookmarks