First of all could someone explain the difference between
var matchingGame = {}; and
var matchingGame = [];
Which of the following is the correct sorting of these 2 ?
If compareFunction(a, b) is less than 0, sort a to a lower index than b.
or
If compareFunction(a, b) is less than 0, sort b to a lower index than a.
If compareFunction(a, b) is less than 0, sort a to a lower index than b.
If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements.
If compareFunction(a, b) is greater than 0, sort b to a lower index than a.
If compareFunction(a, b)is less than zero, sort b to a lower index than a.
If compareFunction(a, b)returns zero, leave a and b unchanged relative to
each other.
If compareFunction(a, b)is greater than zero, sort b to a higher index than a.
Hmmm....
quote 1: If compareFunction(a, b) is less than 0, sort a to a lower index than b.
quote 2: If compareFunction(a, b)is less than zero, sort b to a lower index than a.
I am trying real hard to see your viewpoint, Philip, but I can't.
I don't know how the two could be more diametrically opposed.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
First of all could someone explain the difference between
var matchingGame = {}; and
var matchingGame = [];
Might be better to ask in what ways they are the same. To which the answer is simpler: In no way.
{ } is used to introduce a set of name/value pairs.
As in:
Code:
var sounds = {
dog : "bark",
cat : "meow",
cow : "moo",
"bald eagle" : "squawk!"
};
Note that you can put quotes around then names, but you don't have to unless they violate standard JavaScript naming conventions.
Also note that the above is entirely equivalent to doing:
Code:
var sounds = new Object();
sounds["dog"] = "bark"
sounds.cat = "meow";
sounds.cow = "moo";
sounds["bald eagle"] = "squawk!";
And all of this means that no matter which way you initialize the object, you can later find the value of on object element via
Code:
sounds.cat
or
sounds["cat"]
and so ont.
*************
The square bracket notation is used to create an array of elements (or to declare that a variable is an empty array, if there are no values between the brackets).
Code:
var arr = [ 7, 3, 21, 911 ];
var arr2 = [ "arrays", 3.14159265, "can be", new Date(), "heterogenous" ];
var arr3 = [ ]; // empty array
Array elements are then typically retrieved by element number:
Code:
alert( arr2[1] ); // shows pi
alert( arr2[3] ); // shows time and date when variable was created
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Last edited by Old Pedant; 01-28-2013 at 03:57 AM..
That isn't as accurate as JavaScript can handle pi. If you really wanted to use pi in JavaScript you ought to use Math.PI which contains the most accurate value JavaScript can handle - which should be about 15 decimal places rather than 8.
I'd have expected the Mozilla site to have the correct description for sort without needing to test it - they would be the official standard site for JavaScript if they hadn't given that responsibility to ECMA.
That isn't as accurate as JavaScript can handle pi.
Pbbbbbbbbbbbbbbbbbttttttttttttt!
That's me sticking my tongue out at you.
It was an *EXAMPLE*. A very very SILLY example.
I only wanted to make the point that arrays are heterogenous in JavaScript.
Quote:
I'd have expected the Mozilla site to have the correct description...
Agreed. And I knew it was right. My point really was made in my question: "Why didn't you just try it?"
I'm constantly amazed that people will ask a question in forums where they may have to wait hours or days for an answer when they could just try a smidgen of code themselves and know the answer in minutes, at most.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
One point would be that different browsers COULD in theory implement the function differently, but that'd be really silly. Other than that I couldn't agree more. I dont even answer such questions and if i do i just tell them to try it. If you give them the answer they'll just ask again next time and not learn anything.
Hmmm....
quote 1: If compareFunction(a, b) is less than 0, sort a to a lower index than b.
quote 2: If compareFunction(a, b)is less than zero, sort b to a lower index than a.
I am trying real hard to see your viewpoint, Philip, but I can't.
I don't know how the two could be more diametrically opposed.
Sorry, misread the question.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.