0

I recently undertook an interview in which I was pointed out that I made some objectionable choices such as initializing a javascript array to null values, it was a project that required a fixed sized multidimensional array, that could only be have two values placed in it depending on user action. The values would be of type string.

So I initiated an array like this:

arr: [null, null, null, null, null],

initialize: function() {
 for (var i = 0 ; i < arr.length; i++) {
    arr[i] = [null, null, null, null, null];
 }

}

Understandably, I could have initiated the array with empty strings instead but this was critically pointed out as a questionable choice. Does anybody know why or what?

Parijat Kalia
  • 4,929
  • 10
  • 50
  • 77

4 Answers4

1

Given that there are only 2 potential values for each item in the 2D array, you will avoid allocating extra memory if you actually don't initialize the whole 2D array since JS arrays are essentially hashes (good explanation here: https://stackoverflow.com/a/20323491/2611078).

Initialize array:

var arr = [];

Then, when updating an item after user action:

 if (!arr[i]) { 
     arr[i] = [];
 }
 arr[i][j] = true;

Then, when checking the array:

if (arr[i] && arr[i][j]) {
    console.log('value');
} else {
    console.log('other (default) value');
}
Community
  • 1
  • 1
Julie
  • 1,941
  • 3
  • 17
  • 30
  • What if it is not known that the array has five elements? –  Nov 19 '15 at 19:58
  • you could then initialize the multi-dimensional aspect of the array on the fly -- e.g. initialize `var arr = [];` Then after some user action you could do `arr[8] = []; arr[8][3] = true;` your checks would then have to look like: `if (arr[8] && arr[8][3]) { ... }` – Julie Nov 19 '15 at 19:59
0

See How do you easily create empty matrices javascript? for a discussion of this topic.

I would say the interviewer is right: there is no need for all your nulls.

arr: [null, null, null, null, null],

For this, arr: Array(5) would be fine. Or, you could show off and do [,,,,,].

initialize: function() {
  for (var i = 0 ; i < arr.length; i++) {
    arr[i] = [null, null, null, null, null];
 }

For this, arr[i] = Array(5) would also be fine.

You're just boxing out the dimensions of the two-dimensional array; why put in all the nulls? And definitely not empty strings--that would have gotten you booted out of the interview even more quickly.

Community
  • 1
  • 1
-1

First, I'd say its very strange in JavaScript to initialise an array, you can just set the values of as you need them. Arrays grow as needed.

But if you want to do it the main reason I can see to initialise with empty strings would be that with nulls you are creating a mixed type array, and this is likely to be a performance hit in most JS engines today. But really this is only going to be noticed in a very hot function, meaning its used a lot.

It depends on how you want to use the result, if there are empty strings it might be simpler to use the array later, you can mix the values into a string without checking for null.

mcfedr
  • 7,845
  • 3
  • 31
  • 27
-1

If it is a fixed-sized array (that you know of ahead of time) why not just write the array literal with the type appropriate zero values?

// 5 x 5
function init() {
    return [
      [0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0],
    ];
}

// Or using new Array
function init() {
    return new Array(5).fill(new Array(5));
}
Jonah Williams
  • 20,499
  • 6
  • 65
  • 53
  • What if it is **not** a fixed-size array? –  Nov 19 '15 at 19:58
  • "it was a project that required a fixed sized multidimensional array" - Because the problem specified required one – Jonah Williams Nov 19 '15 at 20:17
  • Sorry, I meant to say, "what if the fixed size is not known ahead of time as you assume"? In other words, the fixed size is given as a parameter? Also, why would you want to initialize them with zeroes? –  Nov 19 '15 at 20:30
  • If the size is not known ahead of time (write time) then its not fixed. – Jonah Williams Nov 19 '15 at 20:37