Skip to main content
deleted 94 characters in body
Source Link
Joseph
  • 25.2k
  • 2
  • 25
  • 37
const Balancer = {
  balance(distribution, goal, places){

    // Reduce the distribution to the sum of its values
    const sum = distribution.reduce((sum, value) => sum + value, 0);

    // For each value in the distribution, get its percentage.
    const percentages = distribution.map(value => value / sum);

    // For each percentage, multiply the goal to get an equally distributed value.
    const goalDistribution = percentages.map(percentage => goal * percentage);

    // For each value, round them off.
    return goalDistribution.map(value => Balancer.round(value, places));
  },
 
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round#PHP-Like_rounding_Method
  round(number, precision) {
    const factor = Math.pow(10, precision);
    const tempNumber = number * factor;
    const roundedTempNumber =return Math.round(tempNumber);
   number return* roundedTempNumberfactor) / factor;
  }
}

var a = [0.33333333331, 0.33333333331, 0.33333333333];
console.log(Balancer.balance(a, 1, 3)); //0.333, 0.333, 0.334

var a = [1, 55, 22, 67];
console.log(Balancer.balance(a, 69699, 0)); //480, 26438, 10575, 32206

var a = [10, 10, 10, 10, 15000];
console.log(Balancer.balance(a, 17000, 0)); //11, 11, 11, 11, 16956


var a = [ 0.00005 ,5 ,0.00005 ,0.00005 ,0.00005 ,0.00005 ,0.00005 ,0.00005 ]
console.log(Balancer.balance(a, -5, 5)); // [-0.00005, -4.99965, -0.00005, -0.00005, -0.00005, -0.00005, -0.00005, -0.00005]


var a = [
        0.0015974718789698097
        ,0.755383581038094
        ,0.13950473043946954
        ,0.0011978091842754731
        ,0.005126875727346068
        ,0.0042250281407886295
        ,0.0001720958819913952
        ,0.0047584144830165875
        ,0.0835073272489086
        ,0.00016098907002300275
        ,0.0028037075787230655
        ,0.0014378579473690483
        ,0.00012411138102484662
    ]
console.log(Balancer.balance(a, 1, 3)); // [0.002, 0.755, 0.14, 0.001, 0.005, 0.004, 0, 0.005, 0.084, 0, 0.003, 0.001, 0]
const Balancer = {
  balance(distribution, goal, places){

    // Reduce the distribution to the sum of its values
    const sum = distribution.reduce((sum, value) => sum + value, 0);

    // For each value in the distribution, get its percentage.
    const percentages = distribution.map(value => value / sum);

    // For each percentage, multiply the goal to get an equally distributed value.
    const goalDistribution = percentages.map(percentage => goal * percentage);

    // For each value, round them off.
    return goalDistribution.map(value => Balancer.round(value, places));
  },
 
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round#PHP-Like_rounding_Method
  round(number, precision) {
    const factor = Math.pow(10, precision);
    const tempNumber = number * factor;
    const roundedTempNumber = Math.round(tempNumber);
    return roundedTempNumber / factor;
  }
}

var a = [0.33333333331, 0.33333333331, 0.33333333333];
console.log(Balancer.balance(a, 1, 3)); //0.333, 0.333, 0.334

var a = [1, 55, 22, 67];
console.log(Balancer.balance(a, 69699, 0)); //480, 26438, 10575, 32206

var a = [10, 10, 10, 10, 15000];
console.log(Balancer.balance(a, 17000, 0)); //11, 11, 11, 11, 16956


var a = [ 0.00005 ,5 ,0.00005 ,0.00005 ,0.00005 ,0.00005 ,0.00005 ,0.00005 ]
console.log(Balancer.balance(a, -5, 5)); // [-0.00005, -4.99965, -0.00005, -0.00005, -0.00005, -0.00005, -0.00005, -0.00005]


var a = [
        0.0015974718789698097
        ,0.755383581038094
        ,0.13950473043946954
        ,0.0011978091842754731
        ,0.005126875727346068
        ,0.0042250281407886295
        ,0.0001720958819913952
        ,0.0047584144830165875
        ,0.0835073272489086
        ,0.00016098907002300275
        ,0.0028037075787230655
        ,0.0014378579473690483
        ,0.00012411138102484662
    ]
console.log(Balancer.balance(a, 1, 3)); // [0.002, 0.755, 0.14, 0.001, 0.005, 0.004, 0, 0.005, 0.084, 0, 0.003, 0.001, 0]
const Balancer = {
  balance(distribution, goal, places){

    // Reduce the distribution to the sum of its values
    const sum = distribution.reduce((sum, value) => sum + value, 0);

    // For each value in the distribution, get its percentage.
    const percentages = distribution.map(value => value / sum);

    // For each percentage, multiply the goal to get an equally distributed value.
    const goalDistribution = percentages.map(percentage => goal * percentage);

    // For each value, round them off.
    return goalDistribution.map(value => Balancer.round(value, places));
  },
  round(number, precision) {
    const factor = Math.pow(10, precision);
    return Math.round(number * factor) / factor;
  }
}

var a = [0.33333333331, 0.33333333331, 0.33333333333];
console.log(Balancer.balance(a, 1, 3)); //0.333, 0.333, 0.334

var a = [1, 55, 22, 67];
console.log(Balancer.balance(a, 69699, 0)); //480, 26438, 10575, 32206

var a = [10, 10, 10, 10, 15000];
console.log(Balancer.balance(a, 17000, 0)); //11, 11, 11, 11, 16956


var a = [ 0.00005 ,5 ,0.00005 ,0.00005 ,0.00005 ,0.00005 ,0.00005 ,0.00005 ]
console.log(Balancer.balance(a, -5, 5)); // [-0.00005, -4.99965, -0.00005, -0.00005, -0.00005, -0.00005, -0.00005, -0.00005]


var a = [
        0.0015974718789698097
        ,0.755383581038094
        ,0.13950473043946954
        ,0.0011978091842754731
        ,0.005126875727346068
        ,0.0042250281407886295
        ,0.0001720958819913952
        ,0.0047584144830165875
        ,0.0835073272489086
        ,0.00016098907002300275
        ,0.0028037075787230655
        ,0.0014378579473690483
        ,0.00012411138102484662
    ]
console.log(Balancer.balance(a, 1, 3)); // [0.002, 0.755, 0.14, 0.001, 0.005, 0.004, 0, 0.005, 0.084, 0, 0.003, 0.001, 0]
added 51 characters in body
Source Link
Joseph
  • 25.2k
  • 2
  • 25
  • 37
  1. The sum of the values in the distribution.
  2. The percentage of each value in the distribution.
  3. The valuedistributed values of each percentage against goal.
  4. The rounded-off version of each distributed value of goal.
  1. The sum of the values in the distribution.
  2. The percentage of each value in the distribution.
  3. The value of each percentage against goal.
  4. The rounded-off version of each value.
  1. The sum of the values in the distribution.
  2. The percentage of each value in the distribution.
  3. The distributed values of goal.
  4. The rounded-off version of each distributed value of goal.
added 51 characters in body
Source Link
Joseph
  • 25.2k
  • 2
  • 25
  • 37

If I understand this program correctly, Balance.balance divides updistributes goal into the same percentage distribution as values in the array and then rounds them off by places? If so, I believethen your code is over-engineered and here's why:

The values are a close match and I'm pretty sure its due to the difference in rounding logic (or my percentagesis due to that initial division). Self-implementing to get the logic can cause errorsdistribution percentages. In this case, the simplest solution would beIf accuracy is a big concern, you might want to consider using third-party numbers library as it will probably be well tested and has been through many eyes and revisionsnumber libraries to maintain accuracy.

It's also obviousapparent you aren't aware of the array methodsnative array methods. ES5 (which is supported by most modern browsers and IE9+) has a lot of array iteration methods you can use to go through an array. In the example, I used array.map and array.reduce which is essentially the bulk of your code.

Your deep clone is not necessary as your array is just 1-Ddimensional. It'sAnd although quick, it's also an expensive operation. Consider shallow copy withusing array.slice instead to shallow-copy arrays. It's not used in the example though,above as array.map creates new arrays with the results and does not reusemutate the existing array.

Your static definition of Balancer is ok, except that the IIFE is unnecessary. IIFE is usually used to hide variables inside the closure. Since you're not doing that, and just returningsimply making Balancer an object, a simple object literal containingwith functions will suffice.

If I understand this program correctly, Balance.balance divides up goal into the same percentage distribution as values in the array then rounds off by places? If so, I believe your code is over-engineered and here's why:

The values are a close match and I'm pretty sure its due to the difference in rounding logic (or my percentages division). Self-implementing the logic can cause errors. In this case, the simplest solution would be a third-party numbers library as it will probably be well tested and has been through many eyes and revisions.

It's also obvious you aren't aware of the array methods. ES5 (which is supported by most modern browsers and IE9+) has a lot of array iteration methods you can use to go through an array. In the example, I used array.map and array.reduce which is essentially the bulk of your code.

Your deep clone is not necessary as your array is just 1-D. It's also an expensive operation. Consider shallow copy with array.slice instead. It's not used in the example though, as array.map creates new arrays with the results and does not reuse the existing array.

Your static is ok, except the IIFE is unnecessary. IIFE is usually used to hide variables inside the closure. Since you're not doing that, and just returning an object, a simple object literal containing functions will suffice.

If I understand this program correctly, Balance.balance distributes goal into the same percentage distribution as values in the array and then rounds them off by places? If so, then your code is over-engineered and here's why:

The values are a close match and I'm pretty sure the difference is due to that initial division to get the distribution percentages. If accuracy is a big concern, you might want to consider using third-party number libraries to maintain accuracy.

It's also apparent you aren't aware of native array methods. ES5 (which is supported by most modern browsers and IE9+) has a lot of array iteration methods you can use to go through an array. In the example, I used array.map and array.reduce which is essentially the bulk of your code.

Your deep clone is not necessary as your array is just 1-dimensional. And although quick, it's also an expensive operation. Consider using array.slice instead to shallow-copy arrays. It's not used in the example above as array.map creates new arrays and does not mutate the existing array.

Your static definition of Balancer is ok except that the IIFE is unnecessary. IIFE is usually used to hide variables inside the closure. Since you're not doing that, simply making Balancer an object literal with functions will suffice.

deleted 4 characters in body
Source Link
Joseph
  • 25.2k
  • 2
  • 25
  • 37
Loading
added 29 characters in body
Source Link
Joseph
  • 25.2k
  • 2
  • 25
  • 37
Loading
Source Link
Joseph
  • 25.2k
  • 2
  • 25
  • 37
Loading