Here is a recursive function, excluding the sorting which should only happen once:

var pointlist = [];
var pointCount = 666;

var generate = function(t, n) {
  for (count = 0; count < n; count++) {
    var point = {
      x: Math.floor(Math.random() * 1000),
      y: Math.floor(Math.random() * 1000)
    };

    t.push(point);
  }
}

generate(pointlist, pointCount);

var divide = function(a) {
  a.sort(function(a, b) {
    return a.x - b.x
  });
  function recurseDivide(a) {
    if (a.length <= 3) return [a];
    var b = a.splice(Math.round(a.length / 2), a.length);
    return recurseDivide(a).concat(recurseDivide(b));
  }
  return recurseDivide(a);
};
var divisions = divide(pointlist);
console.log(divisions.length + " arrays");
console.log(divisions[0].length + " first length");
console.log(divisions[1].length + " second length");</script>

Be aware that after the call to divide, the variable pointlist will have mutated. If you want to avoid this, make the call like this:

var divisions = divide(pointlist.slice());
Answer from trincot on Stack Overflow
Top answer
1 of 2
1

Here is a recursive function, excluding the sorting which should only happen once:

var pointlist = [];
var pointCount = 666;

var generate = function(t, n) {
  for (count = 0; count < n; count++) {
    var point = {
      x: Math.floor(Math.random() * 1000),
      y: Math.floor(Math.random() * 1000)
    };

    t.push(point);
  }
}

generate(pointlist, pointCount);

var divide = function(a) {
  a.sort(function(a, b) {
    return a.x - b.x
  });
  function recurseDivide(a) {
    if (a.length <= 3) return [a];
    var b = a.splice(Math.round(a.length / 2), a.length);
    return recurseDivide(a).concat(recurseDivide(b));
  }
  return recurseDivide(a);
};
var divisions = divide(pointlist);
console.log(divisions.length + " arrays");
console.log(divisions[0].length + " first length");
console.log(divisions[1].length + " second length");</script>

Be aware that after the call to divide, the variable pointlist will have mutated. If you want to avoid this, make the call like this:

var divisions = divide(pointlist.slice());
2 of 2
0

Not recursive but with the help of a useless Array method called Array.prototype.bisect() may be you can do something as follows;

Array.prototype.bisect = function(){
  var atIndex = Math.round(this.length/2);
  return [this.slice(0,atIndex),this.slice(atIndex)];
};
var arr = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19],
    brr = [];
function breakUp(arr){
  while (arr[0].length > 3) arr = [].concat(...arr.map(a => a.length > 3 && a.bisect()));
  return arr;
}
brr = breakUp([arr]);
console.log(JSON.stringify(brr));

๐ŸŒ
The Valley of Code
thevalleyofcode.com โ€บ how-to-divide-array-js
How to divide an array in multiple equal parts in JS
April 10, 2020 - A) The first was to divide the array in equal chunks, for example chunks of 2 or 3 items B) The second was to create n chunks and add an equal variable set of items to it
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Operators โ€บ Division
Division (/) - JavaScript - MDN Web Docs
The division (/) operator produces the quotient of its operands where the left operand is the dividend and the right operand is the divisor.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ division-in-javascript
Division in JavaScript - GeeksforGeeks
July 23, 2025 - In this approach, we are using division using the math library in javaScript. JavaScript makes it simple to divide two variables, and the result is in floating-point integers. However, the Math library, which offers a variety of functions, may be used to get the division's quotient and remainder.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ dividing-an-array-javascript
Dividing an array โ€“ JavaScript
September 18, 2020 - We will Array.prototype.reduce() method over the original array to construct the desired array.
Find elsewhere
๐ŸŒ
Math.js
mathjs.org โ€บ docs โ€บ reference โ€บ functions โ€บ divide.html
math.js | an extensive math library for JavaScript and Node.js
math.divide(2, 3) // returns number 0.6666666666666666 const a = math.complex(5, 14) const b = math.complex(4, 1) math.divide(a, b) // returns Complex 2 + 3i const c = [[7, -6], [13, -4]] const d = [[1, 2], [4, 3]] math.divide(c, d) // returns Array [[-9, 4], [-11, 6]] const e = math.unit('18 km') math.divide(e, 4.5) // returns Unit 4 km
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-get-the-numbers-which-can-divide-all-values-in-an-array-javascript
How to get the numbers which can divide all values in an array - JavaScript
We are required to write a JavaScript function that takes in an array of numbers and returns a number which can exactly divide all the numbers in the array. Letโ€™s say the following is our array โˆ’
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_arithmetic.asp
JavaScript Arithmetic
The modulus operator (%) returns the division remainder.
Top answer
1 of 3
3

If your intent is to produce e.g. ((a / b) / c) given the input array [a, b, c] then you should omit the "initial value" field because there is no "identity" value for the division operator in the way that "0" is the identity to addition and "1" is the identity for multiplication.

function divideArray(array) {
    return array.reduce(function(total, number) {
        return total / number;
    });
}

This will reduce (pun not really intended) to just returning the original value if only a single value is supplied since by definition the reduce function does not call its callback if only a single value is supplied.

With appropriate separation of concerns and simplification of functions to their smallest testable units, your entire code could reduce to:

function _getFieldNumber(product, field) {
    return _extractNumber(helper.getPropertyValue(product, field.trim()));
}

function _getFieldNumbers(product, fields) {
    return fields.map(_getFieldNumber.bind(this, product));
}

function _multipleFields(product, fields) {
    return _getFieldNumbers(product, fields).reduce(multiply, 1);
}

function _divideFields(product, fields) {
    return _getFieldNumbers(product, fields).reduce(divide);
}

function multiply(a, b) { return a * b; }
function divide(a, b)   { return a / b; }
2 of 3
1

As you are using similar functions for multiplication and division, you should try to make them generic.

One way is to get first field and extract number from it and use this as initialValue for .reduce. This way you can keep structure same.

function _operateFields(product, fields, operand) {
  var initial = getNumericValue(product, fields[0])
  var total = fields.slice(1).reduce(function(total, field) {
    var num = getNumericValue(product, field);
    return (operand === "/") ? total / num: total * num
  }, initial);
  return total;
};

function getNumericValue(product, field) {
  return _extractNumber(helper.getPropertyValue(product, field.trim()));
}
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ all-ways-to-divide-array-of-strings-into-parts-in-javascript
All ways to divide array of strings into parts in JavaScript
April 20, 2021 - const arr = ["az", "toto", "picaro", "zone", "kiwi"]; const findAllPossiblities = (arr = []) => { let array; const res = []; for(let i = 1; i < arr.length; i++){ array = []; array.push(arr.slice(0,i).join(" ")); array.push(arr.slice(i).join(" ")); res.push(array); }; return res; }; console.log(findAllPossiblities(arr));
๐ŸŒ
30 Seconds of Code
30secondsofcode.org โ€บ home โ€บ javascript โ€บ math โ€บ quotient and remainder of division
Calculate the quotient and remainder of a division in JavaScript - 30 seconds of code
December 28, 2023 - Its purpose is to return a 2-tuple consisting of the quotient and remainder of a division. For example, divmod(8, 3) returns (2, 2) because 8 / 3 = 2 with a remainder of 2. In order to implement divmod() in JavaScript, we can use the built-in Math.floor() function to get the quotient and the modulo operator (%) to get the remainder of the division x / y.
๐ŸŒ
DEV Community
dev.to โ€บ ticha โ€บ how-to-divide-two-numbers-in-javascript-with-out-using-the-operator-2e8i
How to divide two numbers in JavaScript with out using the "/" operator? - DEV Community
April 15, 2024 - Here're some tips on how to divide numbers in javascript without using the "/" operator. This is a very efficacy method. Which means that it has just a few steps. I will use ES6 syntax for writing my code. const divide = (dividend, divisor) => { if (divisor === 0) { throw new Error("Division by zero is not allowed."); } let quotient = 0; let isNegative = false; if ((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0)) { isNegative = true; } dividend = Math.abs(dividend); divisor = Math.abs(divisor); while (dividend >= divisor) { dividend -= divisor; quotient++; } if (isNegative) { qu
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ javascript-quotient-and-module-of-division-28277
Quotient and Module of Division in JavaScript | LabEx
By the end of this lab, you will have a solid understanding of how to use this function to solve problems involving division in your JavaScript programs. To practice coding, open the Terminal/SSH and type node. This code returns an array that consists of the quotient and remainder of the given ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 8188548 โ€บ splitting-a-js-array-into-n-arrays
javascript - Splitting a JS array into N arrays - Stack Overflow
Imagine I have an JS array like this: var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; What I want is to split that array into N smaller arrays. For instance: split_list_in_n(a, 2) [[1, 2, 3, 4, 5, 6...
๐ŸŒ
Go Make Things
gomakethings.com โ€บ how-to-split-an-array-in-two-or-more-with-vanilla-js
How to split an array in two (or more) with vanilla JS | Go Make Things
August 18, 2022 - The Array.splice() method does a lot. You can use it to delete, replace, and add items to an array at specific indexes.