Apedia

Array Var Trick Cards Future Creates Empty Length

Text When making anki cards for programming:
Try to trick your future self.

Here's another JavaScript fact:

“If the new Array constructor is passed a single number, it creates an empty Array with a length of that number. Any other combination of arguments creates an Array of those arguments.”
You test this out yourself, play around with it, and make two flash cards to try to trick your future self:

var a = new Array('5');
// what is a?

An array with one item, the string '5': ['5'];

... and then an almost-identical question ...

var a = new Array(5);
// what is a?

An empty array with a length of 5.

...another hard example of trying to trick yourself might be...

var a = [20, 10, 5, 1];
// what is a.sort()?

[1, 10, 20, 5]
// sort treats all values as strings

...dont forget to quiz yourself on the solution, too...

var a = [20, 10, 5, 1];
// sort these in numeric order

function compare(v1, v2) { return(v1 - v2); }
a.sort(compare);



Tags: anki

Learn with these flashcards. Click next, previous, or up to navigate to more flashcards for this subject.

Next card: Make string making anki cards programming future solution

Previous card: Operand making anki cards programming turn prose code

Up to card list: Obscure but useful english vocab, logical fallacies and CBT