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?

4
  • Why initialize it to anything besides empty? The loop will take care of populating it to a fixed length. Commented Nov 19, 2015 at 19:07
  • I was initially of the mindset that the array would need a size, in hindsight, yes, I will initialize it to empty. Commented Nov 19, 2015 at 19:09
  • There's also no "fixed" arrays in JS - it can grow and shrink as needed. Commented Nov 19, 2015 at 19:11
  • @tymeJV You can use var a = new Uint8Array(5);a[5]=1;console.log(a); //[0, 0, 0, 0, 0] <-- fixed length Commented Nov 19, 2015 at 19:13

4 Answers 4

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');
}
Sign up to request clarification or add additional context in comments.

2 Comments

What if it is not known that the array has five elements?
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]) { ... }
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.

Comments

-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.

Comments

-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));
}

4 Comments

What if it is not a fixed-size array?
"it was a project that required a fixed sized multidimensional array" - Because the problem specified required one
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?
If the size is not known ahead of time (write time) then its not fixed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.