Bug
The second function does not work. The break
will exit the inner loop before all arrays have been processed. Having a short arrays does not mean the following arrays will also be short.
However looks like you copied it from an answer so not really your code (apart from the bug you added).
Question
"What do you think about my code?"
The first snippet.
- Not too bad.
- Some odd naming.
- A little old school in terms of style.
- A few missing semicolons.
Always create functions
When ever you write code, even as example or experiment write it as a named function with arguments as input, and return the result. This forces you to name the code and changes the way you conceptualize the problem. It may seem like a trivial thing to do, and may not always be useful, however good habits need repetition to pick up.
Number.MIN_VALUE
Javascript has some clangers that should never have been let into the language. This is one of them.
The name suggests MIN as in minimum. One would expect Math.min(Number.MIN_VALUE, anyNumber)
to always return Number.MIN_VALUE
. But no.
Rather it is the smallest positive number greater than 0 or 5e-324 and also happens to the largest negative number if you change the sign -Number.MIN_VALUE === -5e-324
. I wonder what that would be called if they named that.
Number.MIN_VALUE
is as close to useless as you can get. To understand it requires a good working knowledge of Floating point numbers. The minimum practical positive number is Number.EPSILON
and defines the maximum precision of JavaScripts number.
I digress. Your code....
let biggestLength = Number.MIN_VALUE;
Your code works the smallest non empty array has a length of 1. Any number smaller than Number.MIN_VALUE;
would also have worked.
let biggestLength = 0; // would be best for arrays sizes.
Avoid indexing arrays out of bounds.
I can not think of a good reason to do this unless the array has undefined
items within the array length.
You have
if (arr[k][l] !== undefined) {
It works as a test to see if you have gone past the end of the array. But you pay a huge cost. I am not sure why, but indexing outside an arrays bounds is just over 5 times slower than testing against the array size (chrome)
You may say. "Its just a single test how much of a difference can it make."
Well if you change the test to what you actually intended
if (l < arr[k].length) {
Then run the function on 100 arrays of random sizes 1 to 100 the difference is 25%. That means you can process ~125 arrays in the same time as 100.
JavaScript rule of thumb somearray[i] !== undefined
should never replace i < somearray.length
Iterators
You are using for ;;;
type loops to iterate the arrays. Meaning accessing the arrays is via an index, array indexing is harder on the eyes than direct referencing.
Note replacing biggestLength
with theMostGiganticArraysLength
no no sorry, with max
You have
for (let i = 0; i < arr.length; i++) { if (arr[i].length > max) { max = arr[i].length; } }
Use for of
loops if you do not need the item index, the loop becomes...
for (const array of arr) { if (array.length > max) { max = array.length; } }
As you only want length
you could also extract the length via destructuring
for (const {length} of arr) { if (length > max) { max = length; } }
Note there is a small performance penalty using the for of
loop in this case.
Finding max length
The first loop is redundant as you can do that inside the main nested loop. You only need the max value to know when to stop. At minimum you need to look at each array at least once to check length.
You set the max to 1 at the start to make sure you look at each array at least once. As you go check the array length for a max length. After the first inner loop you have the first column of values added and the max array length.
There are many ways you could do this.
function interlacedFlat(arrays) { const result = []; var max = 1; for (let i = 0; i < max; i++) { for (const array of arrays) { if (i < array.length) { result.push(array[i]); if (!i && max < array.length) { max = array.length } } } } return result; }
or
function interlacedFlat(arrays) { const result = []; var max = 1, i = 0; do { for (const array of arrays) { if (i < array.length) { result.push(array[i]); max < array.length && (max = array.length); } } } while (++i < max); return result; }
or
function interlacedFlat(arrays) { const result = []; var max = 1, i = -1; while (i++ < max) { for (const array of arrays) { if (i < array.length) { result.push(array[i]); max = i + 1; } } } return result; }