2

I saw this interesting post yesterday, and thought it'd be important to know how to create a 2D array from sorting the given argument: How to get even numbers array to print first instead of odds?

Below is the code snippet from Ori Drori.

I was curious to know what line of code, and which expression sorts the data and creates 2D array. I assume it's something to do with [numbersArray[i] % 2], but isn't the remainder operator returns the remainder left over?

Also it's a bit confusing as it just set one bracket for an array and use push() to make 2 different arrays.

Any reference that'd help me to understand this will also be much appreciated- thanks!

var numbersArray = [1,2,34,54,55,34,32,11,19,17,54,66,13];

function divider(numbersArray) {
  var evensOdds = [[], []];
  for (var i = 0; i < numbersArray.length; i++) {
    evensOdds[numbersArray[i] % 2].push(numbersArray[i]);
  }
  return evensOdds;
}

console.log(divider(numbersArray));

5
  • 2
    The var evensOdds = [[], []]; initializes a 2d array. The expression numbersArray[i] % 2 result is always 0 or 1, and I use it to decide to which sub array to push the number. Commented Jan 17, 2018 at 20:31
  • 2
    The output data is split into two arrays but both definitely are not sorted. Commented Jan 17, 2018 at 20:31
  • 2
    Do not think about it as 2D. Think about it as an array that consists of two arrays. Commented Jan 17, 2018 at 20:32
  • @OriDrori thanks so much ori! couldn't figure out how to reach you(don't have enough credits ha) so had to write a new question Commented Jan 17, 2018 at 20:43
  • 1
    @bom - welcome :) Commented Jan 17, 2018 at 20:47

2 Answers 2

2

evensOdds has 2 array elements. evensOdds[0] represents first array, which will hold even nums. evensOdds[1] is second element and will hold odd numbers.

When you % 2 an even number, it will result in 0 and 1 for odd number. So when iterating through the array, you % 2, which will return 0 or 1 which enables you to access the first or second array in your evensOdds array and insert it.

The resulting arrays are not sorted but does represent the split between even and odd numbers. To have a sorted results, you will need the following:

var numbersArray = [1,2,34,54,55,34,32,11,19,17,54,66,13];

function divider(numbersArray) {
  var evensOdds = [[], []];
  for (var i = 0; i < numbersArray.length; i++) {
    evensOdds[numbersArray[i] % 2].push(numbersArray[i]);
  }
  return evensOdds.map(array=> array.sort((a,b)=>a-b));
}

console.log(divider(numbersArray));

Sign up to request clarification or add additional context in comments.

1 Comment

now it all makes sense thanks so much! so the first bracket indicates the index number of the evensOdds. you're a life saverrr
1

In the shared code the evensOdds[numbersArray[i] % 2] line is the part that filter the numbers and inserts them in the respective array, using the indexes 0 and 1 returned from the numbersArray[i] % 2 expression:

If it returns 0 so it's an even number and it will be pushed in the first array otherwise if it returns 1 it's an odd number and will be pushed in the second array.

Another alternative:

Well you can simply use Array.filter() method to filter both evens and odds arrays:

Demo:

var numbersArray = [1, 2, 34, 54, 55, 34, 32, 11, 19, 17, 54, 66, 13];

var evens = numbersArray.filter(function(el) {
  return el % 2 == 0;
});

var odds = numbersArray.filter(function(el) {
  return el % 2 == 1;
});

console.log(evens);

console.log(odds);

1 Comment

thank you so much!! yea i didn't realize the first bracket's a convertible one that gives either index0 or 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.